forked from VimalKrishnaRao/OOP-in-Java-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72StringTokenizer.java
More file actions
35 lines (28 loc) · 882 Bytes
/
Copy path72StringTokenizer.java
File metadata and controls
35 lines (28 loc) · 882 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
25
26
27
28
29
30
31
32
33
34
35
import java.io.*;
import java.util.StringTokenizer;
public class Main
{
public static void main(String args[]) throws IOException
{
// Read the line of integers from the file
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
String line = reader.readLine();
reader.close();
// Use the StringTokenizer to parse the line of integers
StringTokenizer tokenizer = new StringTokenizer(line);
// Initialize the sum to zero
int sum = 0;
// Iterate through each token (integer)
while (tokenizer.hasMoreTokens())
{
// Get the next token and parse it as an integer
int num = Integer.parseInt(tokenizer.nextToken());
// Add the integer to the sum
sum += num;
// Print the integer
System.out.println(num);
}
// Print the sum
System.out.println("Sum: " + sum);
}
}