Wednesday, 9 October 2013

JAVA PROGRAMS 5

a)    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:

import java.io.*;
import java.util.*;

class SumOfInt
{
        public static void main(String args[]) throws IOException
        {
                int sum=0;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nEnter String Of Integers:");
                String s=br.readLine();
                StringTokenizer st=new StringTokenizer(s,",");
                while(st.hasMoreTokens())
                {
                        int i=Integer.parseInt(st.nextToken());
                        sum=sum+i;
                        System.out.println(i);
                }
                System.out.println("\nSum Of Integers Is: "+sum);

        }
}
Output:

Enter String Of Integers:12,5,3,1,2
12
5
3
1
2


Sum Of Integers Is: 23

FREE HIT COUNTERS