Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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
Avatar of d-glitch
d-glitch
Flag of United States of America image

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.
Avatar of gudii9

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;
		}
	}


}

Open in new window


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
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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
But i will make sure to format all the time
You should make sure that you/Eclipse don't use <tab> but spaces when indenting.

1. Open Window->Preferences from menu bar.
2. Select Text Editors from tree menu.
3. Uncheck Insert spaces for tabs.
User generated image
After that, (try to) run Format (Ctrl+Shift+F) again.
Avatar of gudii9

ASKER

it was unchecked already for me as attached
preferances.png
Avatar of gudii9

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;
		// }
		// }

	}

}

Open in new window


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