Java Program to Find Sum of Digits in a String

In this blog post, you will see how to Find the sum of digits in a string using Java. A string can have any characters. Here we will find the digit characters in a given string and calculate their sum.

Procedure to calculate the sum of digits in a String:

  1. Declare and Initialize a String variable.
  2. Declare a sum variable to hold the sum of digits.
  3. Use the charAt() method to extract a single character from a given String.
  4. Now use the isDigit() method to check character is a digit or not, if yes add it to the sum variable else ignore it.
  5. Before adding the digit to the sum, convert it to an integer from a character using getNumericValue() method of Character class.
  6. Repeat the 3 to 5 steps until the end of the String.
  7. Finally, print the total sum.

Let’s see a few examples for it below:

Example-1. Calculate the sum of all numbers in a string in Java

package com.javacodepoint.string;

public class SumOfDigitInString {

	// Method to calculate sum of digits
	public static int sumOfDigitInString(String str) {
		// initialize sum variable
		int sum = 0;

		for (int i = 0; i < str.length(); i++) {
			// extract single character from string
			char ch = str.charAt(i);

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

				// convert character to digit and add into sum variable
				int digit = Character.getNumericValue(ch);
				sum = sum + digit;
			}
		}
		// finally return the calculated sum
		return sum;
	}

	// Main method
	public static void main(String[] args) {

		// Declare and Initialize a string (with digits)
		String stringWithDigit = "1ab26xyz88www5";

		// Find the sum
		int sum = sumOfDigitInString(stringWithDigit);

		// Print the result
		System.out.println("The sum of total available digits in the given String= " + sum);

	}

}

OUTPUT:

The sum of total available digits in the given String= 30

Example-2. Sum of digits in a String in Java using Scanner

In this example, we will take string input from the user using java.lang.Scanner class. The next() method of the Scanner class is used to read a string from the user input device. let’s see the java program below:

package com.javacodepoint.string;

import java.util.Scanner;

public class SumOfDigitInString2 {

	// Method to calculate sum of digits
	public static int sumOfDigitInString(String str) {
		// initialize sum variable
		int sum = 0;

		for (int i = 0; i < str.length(); i++) {
			// extract single character from string
			char ch = str.charAt(i);

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

				// convert character to digit and add into sum variable
				int digit = Character.getNumericValue(ch);
				sum = sum + digit;
			}
		}
		// finally return the calculated sum
		return sum;
	}

	// Main method
	public static void main(String[] args) {

		// Scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter a string with digit characters: ");
		String stringWithDigit = sc.next();

		// Find the sum
		int sum = sumOfDigitInString(stringWithDigit);

		// Print the result
		System.out.println("The sum of total available digits in the given String= " + sum);

	}

}

OUTPUT:

Enter a string with digit characters:
java12345codepoint
The sum of total available digits in the given String= 15

See also:
Java Program to Count the Number of Vowels in a String.
Java Program to Count characters from the string in Java.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

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