Any extra lines in the file are simply ignored by the program.
Sometimes a file you wish to use for input starts with a blank line. (This means that the first few characters of the file are the control characters that indicate the end of a line. You can make a file like this with a text editor by hitting "enter" when the cursor is on the first line.) This can be very frustrating. For example, the input file might look like this:
This is line two of the file. The last line of the file.
(The first line is empty, although this is hard to show on this page.) When this file is redirected to the program the output looks like this:
C:\users\default\JavaLessons>java Echo < input.txt Enter your input: You typed: C:\users\default\JavaLessons>
The first line (containing only the end of line characters) is all that is read in. It looks as though the program is not working, but in fact it is doing exactly what it was written to do. When you create data files be careful of this situation.
To read 5 lines from a file,
how many times must the scan.nextLine()
method be executed?