-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseInts.java
More file actions
32 lines (27 loc) · 811 Bytes
/
ParseInts.java
File metadata and controls
32 lines (27 loc) · 811 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
import java.util.Scanner;
// ****************************************************************
// ParseInts.java
//
// Reads a line of text and prints the integers in the line.
//
// ****************************************************************
public class ParseInts {
public static void main(String[] args) {
int val, sum=0;
Scanner scan = new Scanner(System.in);
String line;
System.out.println("Enter a line of text");
Scanner scanLine = new Scanner(scan.nextLine());
while (scanLine.hasNext()) {
try {
val = Integer.parseInt(scanLine.next());
sum += val;
}
catch (NumberFormatException problem) {
System.out.println("Non integer value detected, was skipped.");
}
}
System.out.println("The sum of the integers on this line is "
+ sum);
}
}