go to previous page   go to home page   go to next page highlighting

Answer:

No, Scanner.nextInt() scans in integers as they come down the stream regardless of line structure. However, the examples below have just one integer per line.


Incorrect Input File

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:\temp>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:\temp>

The fifth execution of nextInt() failed because it could not find an integer in the remaining characters of the file.


QUESTION 6:

Here is another defective file:

5
10
20
30
40
50
60
70

What is the problem with this input data?


go to previous page   go to home page   go to next page