Write a Java Program that reads a line of integers, and then displays each integer, and the sum of all the integers (Use StringTokenizer class of java.util)

//Program to print the read numbers and find sum
import java.util.*;
class Sumtoken
{
            public static void main(String args[])
            {
                        Scanner scr=new Scanner(System.in);
System.out.println(“nEnter sequence of integers with space b/w them and press enter : “);
                        String digit=scr.nextLine();
                        StringTokenizer token=new StringTokenizer(digit);
                        int dig=0,sum=0;
                        System.out.println(“nEntered digits are : “);
                        while(token.hasMoreTokens())
                        {
                                    String s=token.nextToken();
                                    dig=Integer.parseInt(s);
                                    System.out.print(dig+” “);
                                    sum=sum+dig;
                        }
                        System.out.println();
                        System.out.println(“Sum is : “+sum);
            }
}
Output:
Enter sequence of integers with space b/w them and press enter :
1 2 3 4 5 6 7 8 9 10
Entered digits are :
1 2 3 4 5 6 7 8 9 10

Sum is : 55

Leave a Reply

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

Enable Notifications OK No thanks