Blank Lines

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

  • How many lines are in the file?    Three
  • How many lines is the program written to read?    One
  • How many lines did the program read?    One

Any extra lines in the file are simply ignored by the program.

Blank Lines

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.

QUESTION 5:

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