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);
}
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: