471,892 Members | 2,104 Online

# Need help with a few javabats

hi, i need help with a couple of javabats.

1) http://javabat.com/prob?id=String3.sumNumbers

Expand|Select|Wrap|Line Numbers
1. public int sumNumbers(String str) {
2.
3.   String retString = "";
4.   String m = "";
5.   int temp = 0;
6.
7.   for (int k = 0; k < str.length(); k++)
8.     {
9.       char c = str.charAt(k);
10.       if (Character.isLetterOrDigit(c))
11.     {
12.       retString += c;
13.     }
14.     }
15.
16.   for (int i = 0; i < str.length(); i++)
17.      {
18.          m = str.charAt(i);
19.
20.          if (Character.isDigit(m))
21.            {
22.               temp += m;
23.            }
24.      }
25.
26.   return temp;
27. }
28.
2) http://javabat.com/prob?id=Array3.canBalance

Expand|Select|Wrap|Line Numbers
1. public boolean canBalance(int[] nums) {
2.
3. int temp = 0;
4.
5. for (int i = 0; i < nums.length; i++)
6. {
7. temp += nums[i];
8. }
9.
10. if (temp % 2 == 0)
11. {
12. return true;
13. }
14. else
15. {
16. return false;
17. }
18.
19. }
20.
3) http://www.javabat.com/prob?id=String3.sameEnds

4) http://javabat.com/prob?id=Array3.squareUp

As you can see, I need help...thanks
Jan 6 '08 #1
10 10006
BigDaddyLH
1,216 Expert 1GB
I'm just looking at the first exercise -- sumNumbers. your code seems rather random, like you copied it from an earlier exercise. First you copy letters and digits to a second string (why?) and do nothing with the resulting string, then you seem to be summing up the digits in the original string. Take a look at one of the examples:

sumNumbers("aa11b33") → 44

If you summed the digits it would be 1 + 1 + 3 + 3 = 8, not 11 + 33 = 44.

I suggest you start with pseudo code -- how you you describe the solution to this operation in simple steps?
Jan 6 '08 #2
I'm just looking at the first exercise -- sumNumbers. your code seems rather random, like you copied it from an earlier exercise. First you copy letters and digits to a second string (why?) and do nothing with the resulting string, then you seem to be summing up the digits in the original string. Take a look at one of the examples:

sumNumbers("aa11b33") → 44

If you summed the digits it would be 1 + 1 + 3 + 3 = 8, not 11 + 33 = 44.

I suggest you start with pseudo code -- how you you describe the solution to this operation in simple steps?

yeah, i forgot to put ret string in the other method. For a string like "7 11", it has a space in it, so I want to condense it. Yeah, i see what u mean.

First I would check each char and see whether or not it is a int. then i have to do something ot check whether or not it is a double digit or a single digit, im having some trouble there
Jan 6 '08 #3
BigDaddyLH
1,216 Expert 1GB
First I would check each char and see whether or not it is a int. then i have to do something ot check whether or not it is a double digit or a single digit, im having some trouble there
Triple digits, quadruple digits, ... "Hello123World4567Example"

keep thinking about it until your proposed solution is clearer to you.
Jan 6 '08 #4
Triple digits, quadruple digits, ... "Hello123World4567Example"

keep thinking about it until your proposed solution is clearer to you.

I check whether or not there are spaces to the left and to the right of it, if there is, its a single digit and i keep checking to get the entire number, or I can check if there is a letter to the left or to the right of it
Jan 6 '08 #5
BigDaddyLH
1,216 Expert 1GB
I check whether or not there are spaces to the left and to the right of it, if there is, its a single digit and i keep checking to get the entire number, or I can check if there is a letter to the left or to the right of it
Why distinguish between a space and a letter? Isn't the key distinction digit or
not a digit?
Jan 7 '08 #6
Why distinguish between a space and a letter? Isn't the key distinction digit or
not a digit?

yeah. i got that. what about the rset?
Jan 7 '08 #7
BigDaddyLH
1,216 Expert 1GB
yeah. i got that. what about the rset?
It's hard to say, based on what you posted. Suppose the input is:

"a123bc45d678"

Imagine doing this exercise not in Java but in your head, making one pass through the string -- as if someone is reading the characters to you one at a time:

a
1
2
3
b
c
4
5
d
6
7
8

How would you do it? Translate that into Java. Here's what would go through my mind:

a -- looking for start of number
1 -- number started, so far "1"
2 -- another digit, so far "12"
3 -- another digit, so far "123"
b -- number ended, number was 123, total so far 123
c -- looking for start of number
4 -- number started, so far "4"
5 -- another digit, so far "45"
d -- number ended, number was 45, total so far 123+45 = 168
6 -- number started, so far "6"
7 -- another digit, so far "67"
8 -- another digit, so far "678"
--end of input, number as 678, total so far 168+678 = 846
Jan 7 '08 #8
It's hard to say, based on what you posted. Suppose the input is:

"a123bc45d678"

Imagine doing this exercise not in Java but in your head, making one pass through the string -- as if someone is reading the characters to you one at a time:

a
1
2
3
b
c
4
5
d
6
7
8

How would you do it? Translate that into Java. Here's what would go through my mind:

a -- looking for start of number
1 -- number started, so far "1"
2 -- another digit, so far "12"
3 -- another digit, so far "123"
b -- number ended, number was 123, total so far 123
c -- looking for start of number
4 -- number started, so far "4"
5 -- another digit, so far "45"
d -- number ended, number was 45, total so far 123+45 = 168
6 -- number started, so far "6"
7 -- another digit, so far "67"
8 -- another digit, so far "678"
--end of input, number as 678, total so far 168+678 = 846
Yeah, I did it like that. It works. Thanks
Jan 7 '08 #9
[ spoonfeeding code deleted ]

Please don't just dump code to a question here. We don't supply boiler plate code.
Read the forum guidelines (select 'Help' near the top right of this page).

kind regards,

Jos (moderator)
Mar 25 '08 #10
JosAH
11,448 Expert 8TB
@OP: I previously let go of it, but now I have to know: what is a 'Javabat'?

kind regards,

Jos
Mar 25 '08 #11

### Post your reply

Sign in to post your reply or Sign up for a free account.

### Similar topics

 reply views Thread by Sofia | last post: by 7 posts views Thread by Mike Kamermans | last post: by 15 posts views Thread by drdoubt | last post: by 9 posts views Thread by sk | last post: by 3 posts views Thread by Bob.Henkel | last post: by 3 posts views Thread by google | last post: by 4 posts views Thread by Phil | last post: by 8 posts views Thread by Sai Kit Tong | last post: by reply views Thread by U S Contractors Offering Service A Non-profit | last post: by reply views Thread by YellowAndGreen | last post: by 2 posts views Thread by beacampos | last post: by reply views Thread by beacampos | last post: by reply views Thread by zermasroor | last post: by 1 post views Thread by Sullster47 | last post: by 1 post views Thread by bsorge | last post: by reply views Thread by beacampos | last post: by reply views Thread by jesuscortes0 | last post: by reply views Thread by slayersays | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.