Room 9 of 15 · about 20 minutes

Token reads and line reads

Room 7 opened a file and room 8 gave you the rule behind the line the compiler asks for. Neither said what a single read takes out of the file. There are two kinds of read, they take different amounts, and every room after this depends on knowing which is which.

Tasks checked0 of 3 XP earned on this path0

What this room checks you can do

Student can choose between Scanner token methods (next, nextInt, nextDouble) and line methods (nextLine) based on the file format, and predict what each call consumes from the buffer.

Notes

Token reading vs. line reading

Scanner has two reading modes that look similar at the call site but behave very differently. A method that picks the wrong mode reads the wrong characters, leaves the wrong characters behind, and produces output that looks "off by one" with nothing at the call site to show why.

Token mode (next(), nextInt(), nextDouble()): reads a chunk of non-whitespace characters, stops at the next whitespace (space, tab, newline). The whitespace itself stays in the buffer for the next call to consume.

Line mode (nextLine()): reads everything up to and including the next newline, returns the line without the trailing newline. The newline is removed from the buffer.

File contents:
    42 17
    -3

| Calls in order | Returned values | What is left in the buffer | |---|---|---| | nextInt(), nextInt(), nextInt() | 42, 17, -3 | (empty) | | nextLine(), nextLine() | " 42 17", " -3" | (empty) | | nextInt(), nextLine() | 42, " 17" | newline + " -3\n" |

The third row is the gotcha that produces the "nextLine returns empty string" bug: after nextInt() reads 42, the buffer still holds the space before 17, the 17 itself, the newline, and the second line. nextLine() then reads up to the first newline, which comes after 17. The return is " 17", not "-3". The trap that follows a token-mode read with nextLine carries the full diagnosis.

When to use which

| Use token mode when | Use line mode when | |---|---| | Values are separated by whitespace and you want them one at a time | The unit of meaning is a whole line (CSV row, log entry, sentence) | | You want type conversion (nextInt, nextDouble) for free | You will parse the line yourself (e.g., line.split(",")) | | You want to skip over blank lines and extra spaces automatically | You need to preserve indentation, embedded spaces, or empty lines |

The CSCD 210 typed-file convention is line mode for the type tag (because it is exactly one line) and token mode for the values (because nextInt/nextDouble parse-for-you).

In other languages

  • Python: for line in f: is line mode; f.read().split() is token mode.
  • C: fgets is line mode; fscanf("%d", ...) is token mode.
  • Bash: read -r line is line mode; read word (with default IFS) is token mode.

What this room assumes you already have

Tasks

Do each one, then check the box. Checking a box is you saying you did it. You can uncheck a box if you check it by accident.

  1. trace
    Show the answer

    42, 17, -3.

  2. trace
    Show the answer

    "42 17", "-3".

  3. write
    Show the answer

    one sc.nextLine() call before the loop; the loop body uses sc.nextInt().

Self check

Type what you think the answer is. Getting it wrong costs nothing and you can try as many times as you want.

Given a file with one blank line between two integer lines, predict whether token-mode reads (nextInt, nextInt) skip the blank line.

Practice, untimed

Open this whenever you want, before the tasks or after them. Nothing in this section is recorded and nothing here is timed.

  1. Given the same file, predict the result of nextInt(), nextLine().trace
    Show the answer

    42, " 17" (note the leading space; nextInt left the rest of the first line in the buffer).

Optional challenge

This one is optional. Do what the room says you can do, without opening any answers, then read the two traps below and check your work against them. Each trap is copied from the notes for this room.

Student can choose between Scanner token methods (next, nextInt, nextDouble) and line methods (nextLine) based on the file format, and predict what each call consumes from the buffer.

How this room finishes

This room is done when all three tasks are checked and the self check is answered.