Java > String-3 > sumDigits (CodingBat Solution)

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


Solution:

public int sumDigits(String str) {
  int len = str.length();
  int sum = 0;
  
  for (int i = 0; i < len; i++) {
    if (Character.isDigit(str.charAt(i))) {
      String tmp = str.substring(i,i+1);
      sum += Integer.parseInt(tmp);
    }
  }
  return sum;
}

12 comments:

  1. public int sumDigits(String str) {
    String integers = "";
    int result = 0;
    for(int count=0; count<str.length(); count++){
    if(Character.isDigit(str.charAt(count))==true){
    integers += Character.toString(str.charAt(count));
    }
    }
    for(int count=0; count<integers.length(); count++){
    result += Integer.parseInt(Character.toString(integers.charAt(count)));
    }
    return result;
    }

    ReplyDelete
  2. public int sumDigits(String str) {
    int sum =0;
    for (int x = 0; x < str.length(); x++){
    if (Character.isDigit(str.charAt(x)))
    sum = sum + Integer.parseInt(str.substring(x,x+1));


    }

    return sum;
    }

    ReplyDelete
  3. public int sumDigits(String str) {
    int sum=0;

    for (int i = 0; i < str.length(); i++)
    {
    if (Character.isDigit(str.charAt(i)))
    {
    sum += Integer.parseInt("" + str.charAt(i));
    }
    }
    return sum;
    }

    ReplyDelete
  4. public int sumDigits(String str) {
    str = str.replaceAll("\\D", "");
    if(str.length()<1) return 0;
    int result=0;
    for(int i=0; i<str.length();i++){
    result = result + Integer.parseInt(str.substring(i,i+1));
    }
    return result;
    }

    ReplyDelete
  5. public int sumDigits(String str) {
    int sum=0;
    if(str.matches(".*\\d.*")){
    int num= Integer.parseInt(str.replaceAll("[^0-9]", ""));
    while (num > 0) {
    sum = sum + num % 10;
    num = num / 10;
    }
    } else{
    sum= 0;
    }
    return sum;
    }

    ReplyDelete
  6. public int sumDigits(String str) {
    str = str.replaceAll("[^\\d]", "");
    int counter = 0;
    for(int i = 0; i < str.length();i++){
    counter = counter + Integer.parseInt(str.substring(i,i+1));
    }
    return counter;
    }

    ReplyDelete
  7. public int sumDigits(String str) {
    int i = 0;
    int result = 0;
    int sum = 0;
    String n1 = "";
    for (i = 0; i < str.length(); i++) {
    if (Character.isDigit(str.charAt(i))) {
    n1+=str.charAt(i);
    result = Integer.parseInt(n1);

    }
    sum += result;
    n1 = "";
    result=0;
    }
    return sum;
    }

    ReplyDelete
  8. for python users :D so low number of python tasks :c

    def sumDigits(s):
    c = 0
    c1 = 0
    for i in range(len(s)):
    if s[i].isnumeric():
    c1 = int(s[i])
    c += c1
    return c

    ReplyDelete
  9. public int sumDigits(String str) {
    int sum = 0;
    for (int i = 0; i < str.length(); i++) {
    char isdigit = str.charAt(i);
    if (Character.isDigit(isdigit)) {
    int j = Character.getNumericValue(isdigit);
    sum += j;
    }
    }
    return sum;
    }

    ReplyDelete
  10. With Java Stream

    public int sumDigits(String str) {
    return str.chars().filter(Character::isDigit).map(i -> Character.digit(i, 10)).sum();
    }

    ReplyDelete
  11. public static int sumDigits(String str) {
    str=str.replaceAll("[^0-9]","");
    int a=0;
    for(int i=0;i<str.length();i++) {
    a+=Integer.parseInt(str.charAt(i)+"");
    }

    return a;
    }

    ReplyDelete