-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSumTok.java
More file actions
27 lines (24 loc) · 829 Bytes
/
SumTok.java
File metadata and controls
27 lines (24 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Java Program that reads a line of integers and then displays each integer
// and sum of all the integers(use string tokenizer class of java.util)
import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;
class SumTok
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit");
int n=sc.nextInt();
sc.nextLine();
System.out.println("Enter the "+ n +" number(space separated)");
String numList=sc.nextLine();
StringTokenizer st = new StringTokenizer(numList);//Splitting the given String
int sum=0;
while(st.hasMoreTokens())
{
sum=sum + Integer.parseInt(st.nextToken());
}
System.out.println("Sum ="+sum);
}
}