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

Answer:

nextLine() returns a String object containing no characters if a line consists of nothing but a newline character.


Blank Lines

Sometimes a file you wish to use for input starts with a blank 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 frustrating, because it is not always clear that the first line is blank. 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:\temp>java Echo < input.txt

Enter your input:
You typed:

C:\temp>

The first line (containing only the end of line character) 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.

If you really want to know what is in a file (of any type) byte by byte, the free program Irfanview is useful. It is mostly an image viewer and manipulator, but has a HEX view feature that shows each byte of a file. Irfanview.

ALL professional programming environments include a HEX editor that shows you exactly what is in a file, byte by byte. Often, it is exactly the tool you need.


QUESTION 5:

To read 5 lines from a file, how many times must the scan.nextLine() method be executed?


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