Series Str
![]() |
Olympus CB-USB5 / USB6 Compatible USB Data Cable w/ Ferrite, Black Sale Price: $0.01 Used From: $2.99 |
|
Start syncing your camera with a PC today through this premium quality data cable.Connect your camera to your PC / Laptop to access and synchronize your pictures.Color: Black.Best Replacement for the original Olympus CB-USB 5 / USB 6 cable... |
Java Easy Program (Plz Help)?
This program is on strings... I thought it would be simple, but I've been on this one for about an hour now... It is just practice for an upcoming test... so PLZ HELP... THANKS IN ADVANCE! (10 easy points for a full answer)
Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)
SAMPLE
sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18
This is the starter:
public int sumNumbers(String str) {
I stumbled across your question and the fact that it was about Java (I just finished a Java course a few months ago) caught my eye. In my class we had a programming assignment similar to yours, except a bit more complicated. We had to parse the Gettysburg Address and perform 4 or 5 different string-related tasks, like counting the words, tallying up the count each letter occurs, etc. I don't have the source in front of me, but can tell a little about what I did, which might help.
IMHO, the problem you have is no quickie, but is fairly straight-forward. You're on the right trace in that you can use isDigit and parseInt to get what you need.
You'll need int variables to retain things like:
(1) the current position as you parse the string
(2) the starting position of a number, once you've found a digit.
(3) the ending position of the number, once you've reached the end of the number and encountered a non-digit.
I used a flag (boolean) variable to keep track of when I had found the first letter of a word and was looking for the end of the word (next char was a punctuation or space).
I used the string.length function to see how long the string is to know when I was at the end of the string.
As you parse the string, when you find a digit you'll need to "look ahead" to find the end of the number. As soon as you find a digit set the starting position to whatever the current position is. Then keep parsing until you encounter a non-digit, then set that position -1 to the ending position of the number.
Having the starting and ending positions, you can use something like a get substring function (exact method escapes me) to get the number and place it in a holding variable (you'll need multipe of these), later to be converted to int and then to tally the total of all the numbers found. Hope this has helped somewhat. Good luck!!
![]() |
![]() Sony ES Series STR DA5400ES Home Theater 71 Channel $1,048.88 Time Remaining: 28d 2h 16m Buy It Now for only: $1,048.88 |



