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, 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 that 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)

5) If the retrieved character is not a digit then go to the next step else convert the character into the digit. For this purpose, we can use getNumericValue() method of the Character class. It returns the int value of the given character.

6) Add the numeric value into the sum variable
7) 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 given string

import java.util.Scanner;

public class SumOfDigitsInString {

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

     // variable to hold character
     char ch ;

     // variable to hold numeric value of ch
     int n;

     // loop to iterate the String
     for(int i=0; i<number.length(); i++) {

         // find character
         ch = number.charAt(i);

         // check character is digit?
         if(Character.isDigit(ch)) {

             // find numeric value of character
             n = Character. getNumericValue(ch);

             // then add it to sum variable
             sum += n;
          }
      }

      // return sum value
      return sum;
  }

  public static void main(String[] args) {

      // declare variables
      String str = null;
      int sum = 0;

      // create Scanner class object 
      // for reading the values
      Scanner scan =  new Scanner(System.in);

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

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

      // display result
      System.out.println("The sum of "
           +" digits in the string "+str
           +" = "+ sum);

      // close Scanner class object
      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


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Similar Java programming examples

Leave a Reply