➤ Sum of digits in a String
➤ Count No of Vowels String
➤ String Pattern Programs in Java
➤ Take String Input In Java
➤ Take Multiple String Input in Java
➤ How To Reverse a String In Java
➤ Remove Special Characters
➤ String – Remove Character
➤ String Palindrome In Java
➤ Sort String In Java
➤ How to Compare Strings In Java
➤ Second Occurrence of Character
➤ Replace nth Occurrence String
➤ Last Occurrence of Character
➤ Uppercase & Lowercase
➤ Java Check If Char is Uppercase
➤ Check If String is Uppercase
➤ Swap Characters in String Java
➤ Java String indexOf() Method
➤ How to Replace Dot in Java?
➤ How to Find Length of String
➤ Substring Method In Java
➤ Split Method In Java
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:-
- Take a String.
- Declare variables to store the sum value, character value, and numeric values of the character.
- 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)
- 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)
- 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. - Add the numeric value to the sum variable.
- 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: Sophia@1999
The sum of digits in the string Sophia@1999 = 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!