This program sums a list of numbers entered by the user. It converts the string representation of each number into an int using parseInt().
Output of Above Java Program
Enter numbers, 0 to quit.
1
Current sum is: 1
2
Current sum is: 3
4
Current sum is: 7
5
Current sum is: 12
8
Current sum is: 20
0
Current sum is: 20
import java.io.*; class ParseDemo { public static void main(String args[]) throws IOException { // create a BufferedReader using System.in BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; int i; int sum=0; System.out.println("Enter numbers, 0 to quit."); do { str = br.readLine(); try { i = Integer.parseInt(str); } catch(NumberFormatException e) { System.out.println("Invalid format"); i = 0; } sum += i; System.out.println("Current sum is: " + sum); } while(i != 0); } }
Output of Above Java Program
Enter numbers, 0 to quit.
1
Current sum is: 1
2
Current sum is: 3
4
Current sum is: 7
5
Current sum is: 12
8
Current sum is: 20
0
Current sum is: 20