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.
This commit is contained in:
parent
e39fd93c81
commit
9c9695f6f1
@ -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{
|
||||||
|
inputBuffer = scanLine.next();
|
||||||
|
if (inputBuffer != null){
|
||||||
|
int val = Integer.parseInt(inputBuffer);
|
||||||
sum += val;
|
sum += val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e){
|
catch (NumberFormatException e){
|
||||||
//pass
|
out.printf("Skipping %s. Not an Integer\n", inputBuffer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out.println("The sum of the integers on this line is " + sum);
|
out.println("The sum of the integers on this line is " + sum);
|
||||||
|
Loading…
Reference in New Issue
Block a user