EE364-lab/lab-03/src/ParseInts.java
HeshamTB e39fd93c81
lab-03:
- try body contains the loop. To study the effect of catching exceptions, the loop will terminate
	at the first presance of a letter. For example, '10 time 4', will only yield a sum of 10.
2020-10-02 16:49:48 +03:00

25 lines
667 B
Java

import java.io.PrintStream;
import java.util.Scanner;
public class ParseInts {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
PrintStream out = System.out;
out.println("Enter a line of text: ");
String line;
Scanner scanLine = new Scanner(in.nextLine());
int sum = 0;
try{
while (scanLine.hasNext()){
int val = Integer.parseInt(scanLine.next());
sum += val;
}
}
catch (NumberFormatException e){
//pass
}
out.println("The sum of the integers on this line is " + sum);
}
}