Write a java program to find Sum of Digits in a String.

Write code to get the sum of all the digits present in the given string. Return the sum as the output. If there is no digit in the given string return -1 as output.

Input and Output format

  • Input consists of a string.
  • The output is a single integer which is the sum of digits in a given string.

Refer to sample output for formatting specifications

Sample Input 1:

good23bad4

Sample Output 1:

9

Sample Input 2:

good

Sample Output 2:

-1

Java Program to find Sum of Digits in a String

Following are the steps to find the sum of digits in a String in Java Programming language.

  • Input a string
  • Call the findSumOfDigits() method with the input string.  
  • Declare and initialize variable sum.
  • Iterate over the string and retrieve each character using the charAt() method.
  • Now, check the character is a digit or not. We are using the Character.isDigit() method of Character class.
  • If it is a digit, then add the value into the sum variable.
  • At last, check the sum value if the sum is equal to 0 then no digits are present in the string else we will return the value in the sum variable.
package com.company;

import java.util.Scanner;

public class SumOfDigitsInString {
    public static void main(String[] args) {
        System.out.println("Enter String: ");
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        findSumOfDigits(s1);
    }

    private static void findSumOfDigits(String s1) {
        int sum = 0;
        for (int i = 0; i < s1.length(); i++)
        {
            char a = s1.charAt(i);
            if (Character.isDigit(a))
            {
                int b = Integer.parseInt(String.valueOf(a));
                sum = sum + b;
            }
        }
        if (sum == 0)
        {
            System.out.println(-1);
        }
        else
            System.out.println(sum);
    }
}

Output

Sum of Numbers in a String in Java using while loop

Following are the steps to find the sum of numbers in a String in Java Programming language:

  • Input string from the user.
  • Convert string to character Array using toCharArray() method.
  • Now, pass this character array to sumOfDigitInString() method.
  • Inside the method, declare and initialize variable i and sum to 0.
  • Now, loop through the array and check if the character is a number or not if it is a number then add it to the sum variable. Repeat this till the length of the array is reached.
  • At last, just print the value of the sum.
package com.company;

import java.util.Scanner;

public class Solution18 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        char c[] = str.toCharArray();
        sumOfDigitInString(c);
    }

    private static void sumOfDigitInString(char[] c) {
        int i = 0,sum =0;
        int len = c.length;
        while(i<len){
            if((c[i] >= '0') && (c[i]) <='9'){
                sum = (sum + c[i] - '0');
            }
            i++;
        }
        System.out.println(sum);
    }
}

Output

In this way, we learn How to find the sum of digits in a string in Java using for and while loop.