Lesson · Console, Arguments & the Runtime Environment
Reading input: Scanner, and what to use instead
10 min readReading Input
The Scanner API everyone learns first, its tokenizer model and famous traps, and the BufferedReader line loop that scales to serious input.
Scanner: convenient, with a model to learn
new Scanner(System.in) wraps the byte stream in token parsing: hasNextInt(), nextInt(), nextLine(), nextDouble(). Its model is a tokenizer over whitespace by default: nextInt reads the next whitespace-delimited token and parses it as an int, and the delimiter can be changed with useDelimiter (so useDelimiter(",") makes CSV fields tokens). Locale can be set too, for decimal-comma inputs.
Scanner is right for teaching, small tools and interactive prompts. It is also slow: it parses incrementally with pattern matching underneath, making it the wrong choice for millions of numbers in competitive settings, where a hand-rolled BufferedReader split typically wins by an order of magnitude.
The newline trap and friends
The famous trap: nextInt() consumes the digits but not the newline that ends the line, so a following nextLine() returns empty. The mental model that fixes it once and for all: Scanner's nextX methods consume a token, nextLine consumes a whole line including up to its newline, and mixing token-reading with line-reading requires acknowledging the leftover line end (an extra nextLine() call does it).
Sibling traps: next() returns a token, nextLine() a whole line (different enough that calling the wrong one fails silently on single-word input); hasNextInt returning false on invalid input leaves the bad token in place, so a naive loop spins forever; and Scanner never throws on parse failure, it declines: check hasNextX or catch InputMismatchException.
The line loop
The workhorse pattern, and the one to default to: BufferedReader reading line by line, splitting each line when it has structure, parsing when it has numbers. while ((line = reader.readLine()) != null) is the canonical loop; null is the end of stream, and readLine strips the line terminator for you.
For one value per line, Integer.parseInt(line.trim()) is direct and fast. For many values per line, line.split(" ") allocates strings; StringTokenizer or an index walk over the characters avoids that. In a world with java.util.Scanner and streams, the string splitting dominates for structured bulk input, and the explicit charset (UTF-8 by default since 18) keeps the decoding honest.
Console and arguments, the other two doors
System.console() exists for the interactive-terminal cases Scanner handles badly: it detects whether a real console is attached (returns null when not), offers readLine prompts and readPassword without echo. Programs do not get to assume it exists, which is why argument or stream design beats prompting wherever automation might appear.
Command-line arguments, covered properly in the next topic, are the other door in. A program that reads a file path from args and reports errors on stderr is composable with pipes and scripts, which is why tools evolve from Scanner-and-prompts to streams-and-arguments as they grow up.
The newline trap has one sentence: nextInt takes the number, not the line
Scanner's nextInt stops at the newline and leaves it queued. The next nextLine reads that leftover, which is empty, and your prompt-skipping bug is born. Fix: read one extra nextLine() after any token read before switching to line reads, or standardise on nextLine plus parseInt and never mix.
Interview angles
Why does Scanner need an extra nextLine after nextInt?+
nextInt consumes only the number token; the line's terminator stays in the buffer and the following nextLine returns empty. The models differ (token versus line) and the buffer is shared, so mixing them needs the discard call.
Scanner versus BufferedReader?+
Scanner parses tokens with type-aware methods and is slowish, ideal for small interactive input. BufferedReader reads lines fast with no parsing; you split and parse yourself, ideal for bulk input. Competitive code prefers the second by a large margin.
What does readLine return at end of stream?+
Null, which terminates the canonical while loop. Empty string is a real (blank) line; null means there are no more lines. Distinguishing the two is the whole error handling of the loop.