473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 10211
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

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

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
15
by: drdoubt | last post by:
using namespace std In my C++ program, even after applying , I need to use the std namespace with the scope resolution operator, like, std::cout, std::vector. This I found a little bit...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
3
by: Bob.Henkel | last post by:
I write this to tell you why we won't use postgresql even though we wish we could at a large company. Don't get me wrong I love postgresql in many ways and for many reasons , but fact is fact. If...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.