06 September, 2012

Use StringTokenizer class

Java program that displays input integer and the sum of all the integers 

( use StringTokenizer class )



import java.io.*;
import java.lang.*;
import java.util.*;
class StringTokenizerExample
{
  public static void main(String args[])
 {
  int n;
  int sum=0;
  Scanner s=new Scanner(System.in);
  System.out.println("Enter integer seperated by space");
  String str=s.nextLine();
  StringTokenizer st=new StringTokenizer(str," ");
  while(st.hasMoreTokens())
  {
    String temp=st.nextToken();
    n=Integer.parseInt(temp);
    System.out.println(n);
    sum=sum+n;
   }
    System.out.println("sum="+sum);
 }
}

No comments:

Post a Comment

Thank You !