- Correctly handle in catch body with input buffer defined outside the try body to accessed by catch body
	and print the skipped word.
This commit is contained in:
HeshamTB 2020-10-02 17:01:51 +03:00
parent e39fd93c81
commit 9c9695f6f1
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -9,14 +9,18 @@ public class ParseInts {
String line; String line;
Scanner scanLine = new Scanner(in.nextLine()); Scanner scanLine = new Scanner(in.nextLine());
int sum = 0; int sum = 0;
try{ String inputBuffer = "";
while (scanLine.hasNext()){ while (scanLine.hasNext()){
int val = Integer.parseInt(scanLine.next()); try{
sum += val; 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);
} }
}
catch (NumberFormatException e){
//pass
} }
out.println("The sum of the integers on this line is " + sum); out.println("The sum of the integers on this line is " + sum);