- 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:
HeshamTB 2020-10-02 16:49:48 +03:00
parent d1f38a542b
commit e39fd93c81
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -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;
try{
while (scanLine.hasNext()){ while (scanLine.hasNext()){
try {
int val = Integer.parseInt(scanLine.next()); int val = Integer.parseInt(scanLine.next());
sum += val; sum += val;
} }
}
catch (NumberFormatException e){ catch (NumberFormatException e){
//pass //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);
} }
} }