gudii9
asked on
sumNumber challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p121193
I am not clear on challenge desciption and how to approach. please advise
I am working on below challenge
http://codingbat.com/prob/p121193
I am not clear on challenge desciption and how to approach. please advise
Is this homework, gudii9, or what?
We won't (and are not allowed to) write the code for you.
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.
At least come up with some code you wrote yourself.
Starting from that we can (try to) help you further.
ASKER
@tel2
it is not a home work. And i am not a student. i wonder what made you to think it is a homework?
it is not a home work. And i am not a student. i wonder what made you to think it is a homework?
ASKER
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.)
sumNumbers("abc123xyz") → 123//how 1+2+3 is 123 it should be 6 right??same way others?
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18
Hi gudii9,
> "i wonder what made you to think it is a homework?"
Because of the way you ask.
Please tell me:
a) What are you if you are not a student?
b) Why do you want to learn Java?
c) How did you decide on these questions?
Thanks.
tel2
> "i wonder what made you to think it is a homework?"
Because of the way you ask.
Please tell me:
a) What are you if you are not a student?
b) Why do you want to learn Java?
c) How did you decide on these questions?
Thanks.
tel2
No need to repeat the challenge description. We're grown ups, we can click on a link.
Show us that you can write some lines of Java code...
Show us that you can write some lines of Java code...
ASKER
sumNumbers("abc123xyz") → 123//how 1+2+3 is 123 it should be 6 right??
sumNumbers("aa11b33") → 44//how 1+1+3+3 is 44 it should be 8 right??
sumNumbers("7 11") → 18//how 7+1+1 is 18 it should be 9 right??
please advise
sumNumbers("aa11b33") → 44//how 1+1+3+3 is 44 it should be 8 right??
sumNumbers("7 11") → 18//how 7+1+1 is 18 it should be 9 right??
please advise
A Number is a sequence of digits
The string "abc123xyz" only contains one number: 123
The string "aa11b33" contains two numbers: 11 and 33, their sum is 44
You got the idea I think.
The string "abc123xyz" only contains one number: 123
The string "aa11b33" contains two numbers: 11 and 33, their sum is 44
You got the idea I think.
ASKER
now i got. let me think how to code
ASKER
i think i need to 2 for loops to compare each character with rest of charactes. let me think more
ASKER
public int sumNumbers(String str) {
int sum=0;
for(int i = 0; i < str.length(); i++)
{
char c = str.charAt(i);
if(Character.isDigit(c)){
sum=sum + Integer.parseInt(String.valueOf(c));
}
}
return sum;
}
passes half test
Expected Run
sumNumbers("abc123xyz") → 123 6 X
sumNumbers("aa11b33") → 44 8 X
sumNumbers("7 11") → 18 9 X
sumNumbers("Chocolate") → 0 0 OK
sumNumbers("5hoco1a1e") → 7 7 OK
sumNumbers("5$$1;;1!!") → 7 7 OK
sumNumbers("a1234bb11") → 1245 12 X
sumNumbers("") → 0 0 OK
sumNumbers("a22bbb3") → 25 7 X
other tests
X
Your progress graph for this problem
ASKER
public class SumNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("sum is--->" + sumNumbers("7 11"));
}
public static int sumNumbers(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (Character.isDigit(c)) {
sum = sum + Integer.parseInt(String.valueOf(c));
} else {
i = i + 1;
}
}
return sum;
}
}
gives worn output
sum is--->8
ASKER
need to make my solution more universal so that i can move exact distance not 1 all the time
ASKER
not able to maker it work with 2 for loops.
below passed all tests
below passed all tests
public int sumNumbers(String str) {
int len = str.length();
int sum = 0;
String tmp = "";
for (int i = 0; i < len; i++) {
if (Character.isDigit(str.charAt(i))) {
if (i < len-1 && Character.isDigit(str.charAt(i+1))) {
tmp += str.charAt(i);
}
else {
tmp += str.charAt(i);
sum += Integer.parseInt(tmp);
tmp = "";
}
}
}
return sum;
}
ASKER
public int sumNumbers(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
int count = 0;
for (int j = i; j < str.length(); j++) {
if (Character.isDigit(str.charAt(j))) count++;
else break;
}
sum += Integer.parseInt(str.substring(i, i + count));
i += count;
}
}
return sum;
}
above inner for loop why we need to start j=1 rather than j=0 similar to outer for loop(where i started with 0)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.