EE364-lab/lab-03/src/ParseInts.java
HeshamTB 9c9695f6f1
lab-03:
- Correctly handle in catch body with input buffer defined outside the try body to accessed by catch body
	and print the skipped word.
2020-10-02 17:05:28 +03:00

29 lines
874 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;
String inputBuffer = "";
while (scanLine.hasNext()){
try{
inputBuffer = scanLine.next();
if (inputBuffer != null){
int val = Integer.parseInt(inputBuffer);
sum += val;
}
}
catch (NumberFormatException e){
out.printf("Skipping %s. Not an Integer\n", inputBuffer);
}
}
out.println("The sum of the integers on this line is " + sum);
}
}