lab-03:
- try body contains the loop. To study the effect of catching exceptions, the loop will terminate at the first presance of a letter. For example, '10 time 4', will only yield a sum of 10.
This commit is contained in:
parent
d1f38a542b
commit
e39fd93c81
@ -9,15 +9,16 @@ 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;
|
||||||
while (scanLine.hasNext()){
|
try{
|
||||||
try {
|
while (scanLine.hasNext()){
|
||||||
int val = Integer.parseInt(scanLine.next());
|
int val = Integer.parseInt(scanLine.next());
|
||||||
sum += val;
|
sum += val;
|
||||||
}
|
}
|
||||||
catch (NumberFormatException e){
|
|
||||||
//pass
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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