Scanner.nextInt() will scan in integers as they come
regardless of line structure.
However, the examples will show one text integer per line.
Professionally written code carefully checks the input for errors. We won't do that, but will rely on what comes automatically with Java. Say that the input file looked like this:
5 1 2 3 4
The file is incorrect since it says that five integers are to be added up, but only four integers follow. The line after the "4" is blank. Here is what happens if you run the program with this file:
C:/users/default/JavaLessons>java AddUpAll File name? data.dat Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at AddUpAll.main(AddUpAll.java:24) C:/users/default/JavaLessons>
The fifth execution of
failed because it could not find an integer in the remaining characters of the file.
Here is another defective file:
5 10 20 30 40 50 60 70
What is the problem with this input data?