gudii9
asked on
sumDigits challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p197890
I am not clear on challenge desciption and how to approach. please advise
I am working on below challenge
http://codingbat.com/prob/p197890
I am not clear on challenge desciption and how to approach. please advise
Try a little harder please
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
public class SumDigits {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
*
* Given a string, return the sum of the digits 0-9 that appear in the
* string, ignoring all other characters. Return 0 if there are no
* digits in the string. (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.)
*
* sumDigits("aa1bc2d3") → 6 sumDigits("aa11b33") → 8
* sumDigits("Chocolate") → 0
*/
System.out.println("i is-->" + sumDigits("aa1bc2d3"));
System.out.println("i2 is-->" + sumDigits("Chocolate"));
}
public static int sumDigits(String str) {
for (int i = 0; i < str.length() - 1; i++) {
if (str.charAt(i) =='1')){
return 1;
}
if (str.charAt(i) =='2')){
return 1;
}
if(str.charAt(i)=='3')){return 1;
}
else{
return 0;
}
}
}
i tried as above but getting lot of errors. i know how to do in my mind not able to put in java language compiler understands. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It's rather clear that returning 1 when you encounter the digit '2' or '3' is wrong and won't work.
You have to return the SUM of all digit characters.
So somewhere you'll have to ADD values and when all are added THEN return the sum.
i will try to sum it.
I pressed control shift F but eclipse refused to format some reason. But i will make sure to format all the time
ASKER
it was unchecked already for me as attached
preferances.png
preferances.png
ASKER
public class SumDigits {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
*
* Given a string, return the sum of the digits 0-9 that appear in the
* string, ignoring all other characters. Return 0 if there are no
* digits in the string. (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.)
*
* sumDigits("aa1bc2d3") ? 6 sumDigits("aa11b33") ? 8
* sumDigits("Chocolate") ? 0
*/
System.out.println("i is-->" + sumDigits("aa1bc2d3"));
System.out.println("i2 is-->" + sumDigits("Chocolate"));
}
public static int sumDigits(String str) {
/*
* char[] array = reader.toCharArray(); for (char ch : array) {
* System.out.println (ch);
*/
/*
* for (char ch: str.toCharArray()) {
*
*
*
* for (int i = 0; i < str.length() - 1; i++) { if
* ((Character.isDigit(ch[i])){ return 1; }
*/
// String s = "xyz";
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;
// if (str.charAt(i) =='2')){
// return 1;
// }
//
// if(str.charAt(i)=='3')){return 1;
// }
// else{
// return 0;
// }
// }
}
}
i passed all
Expected Run
sumDigits("aa1bc2d3") → 6 6 OK
sumDigits("aa11b33") → 8 8 OK
sumDigits("Chocolate") → 0 0 OK
sumDigits("5hoco1a1e") → 7 7 OK
sumDigits("123abc123") → 12 12 OK
sumDigits("") → 0 0 OK
sumDigits("Hello") → 0 0 OK
sumDigits("X1z9b2") → 12 12 OK
sumDigits("5432a") → 14 14 OK
other tests
OK