Write a Java program that reads a line of integers and then displays each integer, and the sum of all the integers( Use String Tokenizer class of java.util)
Program Code:
import java.io.*;
import java.util.*;
class StringToken
{
public static void main(String[] args) throws Exception
{
int i=0,sum=0;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the Line of Integer Separated by Space");
String str=dis.readLine();
int n=str.length();
int a[]=new int[n];
StringTokenizer st=new StringTokenizer(str);
while(st.hasMoreTokens())
{
a[i]=Integer.parseInt(st.nextToken());
System.out.println("a["+i+"]= "+a[i]);
i++;
}
for(i=0;i<n;i++)
sum+=a[i];
System.out.println("Sum is = "+sum);
}
}
Post A Comment:
0 comments: