473,394 Members | 1,171 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,394 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 10213
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.