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