Java Program to Find Sum of Digits in a String

Java Program to Find Sum of Digits in a String | In this post, we will develop a Java program to find the sum of digits in a string. The String can hold any character. We have to find the characters which are alphabets and then calculate their sum value.

Procedure to find the sum of digits in a string Java:-

  1. Take a String.
  2. Declare variables to store the sum value, character value, and numeric values of the character.
  3. Retrieve the character. To retrieve the character we can use charAt() method of the String class. The prototype of the charAt() method is:- public char charAt(int index)
  4. Now, check if the character is a digit or not. We can manually check it using the ASCII value of 0 to 9, or we can use isDigit() method of the Character class.

There are two overloading forms of the isDigit() method. The prototype of the isDigit() method of Character class are:-

  • public static boolean isDigit(char ch)
  • public static boolean isDigit(int codePoint)
  1. If the retrieved character is not a digit then go to the next step else convert the character into a digit. For this purpose, we can use getNumericValue() method of the Character class. It returns the int value of the given character.
  2. Add the numeric value to the sum variable.
  3. Repeat the 3 to 6 steps until the end of the String. To find the length of the string use length() method of the String class.

Java Program to Find the Sum of Digits in a String

import java.util.Scanner;

public class SumOfDigitsInString {

    private static int sumOfdigits(String number) {
        // variable to store sum of digits
        int sum = 0;

        for (int i = 0; i < number.length(); i++) {
            // find character
            char ch = number.charAt(i);
            // check character is digit?
            if (Character.isDigit(ch)) {
                // find numeric value of character
                int value = Character.getNumericValue(ch);
                // then add it to sum variable
                sum += value;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // read inputs
        System.out.print("Enter String: ");
        String str = scan.next();

        // find sum of digits in the String
        int sum = sumOfdigits(str);

        System.out.println("The sum of "
                   + " digits in the string " + str
                   + " = "+ sum);
        scan.close();
    }
}

The output for the different test cases:-

Enter String: 12345
The sum of digits in the string 12345 = 15

Enter String: 09851
The sum of digits in the string 09851 = 23

Enter String: hello2021
The sum of digits in the string hello2021 = 5

Enter String: [email protected]
The sum of digits in the string [email protected] = 28

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *