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{
|
||||||
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user