Java Program to Count The Sum of Numbers in a String
Count the sum of number in a string
For counting the sum of a digit in a given string, we’re going to code a java program in this article. Take a string input from the user and store it in a variable called “s” in this case. We are using isdigit method of Character class to check that particular character is a digit or not if that character is a digit then add that character to the sum variable to find sum.
Here we need to count the sum of the number present in the string in the string we can have alphabets as well as some digits so we need to find the sum of the digits. Lets take one example to understand this.
- Input string :- “4PREP2INSTAA6”
- Output :- 12
As in the given string “4PREP2INSTAA6” the number of character that is a number is 4+2+6=12 hence the output is 12.
First we can iterate through the string and check if the character is in range of ‘0’ to ‘9’ if that is the case we will add it to the sum and then print the result.
Using inbuild function we can do the same that is iterate throughout the string and check if the character is a digit or not using isdigit() function.
Algorithm
- Take a string input from user and store it in the variable called “s”.
- Take a i for loop start from i=0 to i<s.length().
- Check the condition that character is digit or not.
- Calculate sum of each digit and store it in the sum variable.
- After that simply print the value of sum.
Java Code (Count the sum of number in a string)
Run
import java.util.Scanner; public class Main { public static void main(String[] args) { String s ="4PREP2INSTAA6"; int sum=0; for (int i = 0; i < s.length(); i++) { if(Character.isDigit(s.charAt(i))) sum=sum+Character.getNumericValue(s.charAt(i)); } System.out.println("Sum of all the digit present in String : "+sum); } }
Output
Sum of all the digit present in String : 12
Method 2
Run
public class Main { public static void main(String[] args) { String str="4PREP2INSTAA6";int sum=0; for(int i=0;i<str.length();i++){ if(str.charAt(i)>='0' && str.charAt(i)<='9'){ sum+=(str.charAt(i)-'0'); } } System.out.println("Sum of all digits " +sum ); } }
Output
Sum of all digits 12
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
import java.util.*;
public class exercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int count=0;
for(int i=0;i<str.length()-1;i++){
count++;
}
System.out.print(count);
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
System.out.println(“Enter string”);
String str=scan.nextLine();
int sum=0;
for(int i=0;i=’0′ && ch<='9')
{
sum=sum+(ch-'0');
}
}
System.out.println("Sum="+sum);
}
}
public class SumOfNumInString {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String str1 =””;int l;int sum=0;
str1 = str.replaceAll(“[^0-9]”, “”);
int n = Integer.parseInt(str1);
while(n>0) {
l=n%10;
sum=sum+l;
n=n/10;
}
System.out.println(sum);
}
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println(“enter String”);
String s=sc.next();
int i,ch,sum=0;
String s1=””;
for(i=0;i
=’0’&&ch<='9'){
s1=s1+(char)ch;
sum=sum+Integer.parseInt(s1);
}
s1="";
}
System.out.println(sum);
}
}
import java.util.Scanner;
public class p10 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(“Enter a String”);
String s1 = sc.nextLine();
int sum=0;
s1=s1.replaceAll(“[^0-9]”,””);
System.out.println(s1);
Integer number = Integer.valueOf(s1); //convert from String to integer
for(int i =0;i< s1.length();i++){
sum= sum +number % 10;
number=number/10;
}
System.out.println(sum);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
int sum = 0;
for(int i=0; i
=’0′ && s.charAt(i)<='9'){sum = sum + (s.charAt(i) – '0');
}
}
System.out.println(sum);
}
String str = “pr2ep4in5st1”;
int res = 0;
str = str.replaceAll(“[^0-9]”,””);
char[] ch = str.toCharArray();
for(int i = 0;i<ch.length;i++){
res = res + Character.getNumericValue(ch[i]);
}
System.out.println("res);
I think this is easier !!
String str = “pr2ep4in5st1”;
int res = 0;
str = str.replaceAll(“[^0-9]”,””);
char[] ch = str.toCharArray();
for(int i = 0;i<ch.length;i++){
res = res + Character.getNumericValue(ch[i]);
}
System.out.println(" "+res+" ");
I think this is easier !!
public class MyClass {
public static void main(String args[]) {
String s=”pr2ep4in5st1″;
int sum=0;
for(int i=0;i<s.length();i++)
{
if(Character.isLetter(s.charAt(i)))
{
continue;
}
else
{
sum+=Integer.parseInt(""+s.charAt(i));
}
}
System.out.println(sum);
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println(“enter a string:”);
String str=obj.nextLine();
int n=str.length();
int s=0;
s=s+n;
System.out.println(“sum:”+s);
}
}