Question: Trying to write a Java program code for this problem.. Given a string, return the sum of the digits 0-9 that appear in the string,

Trying to write a Java program code for this problem..

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

Use the following method signature:

public int sumDigits(String str) {

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!