I have a String that accepts numbers from user input and I want to calculate the sum of all the numbers in a String.
An example of a sample output will look like this,
Enter an integer: 1234 The sum is 10.
I have a String that accepts numbers from user input and I want to calculate the sum of all the numbers in a String.
An example of a sample output will look like this,
Enter an integer: 1234 The sum is 10.
String s = JOptionPane.showInputDialog("Enter an integer");
int sum = 0;
for(int i = 0; i < s.lentgth(); i++){
sum+=Integer.parseInt(s.charAt(i)+"");
}
System.out.println("Sum is: " + sum);