Skip to content
ExamSpine

Lesson 6.8

Line by line

Part 6: Errors, testing and filesReading, about 7 min2 exercises in this lesson

Java answers can't run on ExamSpine yet. You can still read Java, trace it, and put an answer's lines in order.

Line by line

By the end of this you will be able to read a file one line at a time, do something with each line, and stop at the end of the file, however long it is.

In the last lesson the program knew the file had two lines, so it read twice. Most files are not like that: a class list or a log can have any number of lines. A loop reads them all.

How it is asked

As with the last lesson, no past paper has a file question yet. But this loop is under almost every file task you could be set, and under every lesson that follows: read every line, and count, total, pick out or print something from each.

Key idea: Read a line, test for the end of the file, use the line, read the next: one line per turn, however long the file is.

A loop over the lines

names.txt holds one name per line. The program finds the longest name. Step through it and watch name take each line in turn.

This is the pattern in both languages: read one line before the loop, keep going while the last read found a line, and read the next line as the last thing in the loop body. In Python the end of the file shows up as empty text, "", and in Java as null. An empty line in the middle of the file is not "" in Python, because it still has its line end.

Python also has a shorter way to write the same loop, and you will use it most of the time:

for line in f:
    name = line.rstrip("\n")

for line in f: reads one line per turn and stops by itself after the last one. Each line still keeps its line end, so take it off with rstrip("\n") (or strip(), which also removes spaces at both ends).

The read happens twice

That pattern has two reads: one before the loop and one at the end of each turn. Leaving out either one breaks the loop.

The opposite slip is reading twice in one turn, for example once in the while condition and again in the body. Then every other line is skipped.

Blank lines and the last line

Two things in real files catch people out.

  • A blank line is still a line. The loop gets it as empty text (after the line end is taken off), so a count of lines counts it too.
  • The last line may have no line end after it. Python's rstrip("\n") does nothing to it, and Java's readLine() still gives it back, so the loop above reads it like any other.

How it is marked

Here is an exam-style question of our own, marked one mark for each point a correct answer must show, as IB computer science markschemes mark code. Switch to the second script to see two slips.

Now try it yourself on the questions below.

Exercise 6.8.16.8.1 · Write itNumber the lines of a poem

Write a program that reads the file poem.txt and prints every line of it with its line number in front, then how many lines there were.

  1. For each line, in order, print the line number, then : , then the line exactly as it is in the file. The first line is number 1.
  2. Some lines start with spaces, to indent them. Keep those spaces.
  3. An empty line is still a line. It gets a number like the others.
  4. After the last line, print Lines: followed by the number of lines. A file with nothing in it has 0 lines.
  5. The last line may or may not end with a line end.

Your program is run several times, each time in a folder that holds only the files shown for that run. Spaces at the end of a printed line are ignored.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 7 times: the 2 examples below, and 5 hidden ones that check the rules above. Only what it prints is checked. Each run starts in an empty folder that holds only the files shown for it.

  • two lines, numbered from 1, then the count (rules 1 and 4)

    You type

    Nothing

    It prints

    1: Rain on the roof
    2: and on the road
    Lines: 2

    Files in its folder

    poem.txt
    Rain on the roof
    and on the road
  • an indented line keeps its spaces (rule 2)

    You type

    Nothing

    It prints

    1: The tide
    2:   comes in
    3: slowly
    Lines: 3

    Files in its folder

    poem.txt
    The tide
      comes in
    slowly
solution.pySolution.java

Exercise 6.8.26.8.2 · Write itPick the warnings out of a log

A program writes a log file, log.txt, with one message per line. Write a program that prints only the warnings, then how many there were.

  1. A warning is a line whose first four characters are WARN, in capital letters, whatever follows them: WARN disk full, WARNING low and WARN:fan are all warnings. A line that has WARN somewhere later, or starts with warn or Warn, is not a warning.
  2. Print each warning exactly as it is in the file, in the order they appear.
  3. After the last line of the file, print Warnings: followed by the number of warnings. If there are none, that is Warnings: 0.
  4. The file may be empty. A line may be empty or shorter than four characters. The last line may or may not end with a line end. No line starts with a space.

Your program is run several times, each time in a folder that holds only the files shown for that run. Spaces at the end of a printed line are ignored.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 8 times: the 2 examples below, and 6 hidden ones that check the rules above. Only what it prints is checked. Each run starts in an empty folder that holds only the files shown for it.

  • two of the four lines start with WARN (rules 1 to 3)

    You type

    Nothing

    It prints

    WARN disk 91% full
    WARN battery low
    Warnings: 2

    Files in its folder

    log.txt
    INFO started
    WARN disk 91% full
    INFO saved 3 files
    WARN battery low
  • one warning, on the last line (rules 2 and 3)

    You type

    Nothing

    It prints

    WARN low ink
    Warnings: 1

    Files in its folder

    log.txt
    INFO all fine
    WARN low ink
solution.pySolution.java

Report a problem with this page