Wednesday, 13 March 2013

Java program that reads a line of integers and then display each integer and the sum of all integers



/* Java program that reads a line of integers and then display each integer and the sum of all integers (Use StringTokenizer Class Of java.util)*/

// File Name: sumofintegers.java

import java.io.*;
import java.util.*;
class sumofintegers {
public static void main(String arg[]) throws IOException {
int sum=0,n;
String s;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter The Integers Saperating Them With Space ");
s=in.readLine();
StringTokenizer token =new StringTokenizer(s);
while(token.hasMoreTokens())
{
n=Integer.parseInt(token.nextToken());
sum+=n;
System.out.println(n);
}
System.out.println("Sum Of All The Integers Is : "+sum);
System.out.println("www.shashanktechnologies.blogspot.com");
}
}


Output:

Compilation: G:/shashank>javac sumofintegers.java

Interpretation: G:/shashank>java sumofintegers





2 comments:

  1. you have to use scanner instead of datastreaminput and nextLine instead of readLine

    ReplyDelete