Sub topic 13
Choosing the Right Approach
This chapter assumes you know Java classes, methods, variables, control flow, String, primitive types, and basic exception handling. You should also know how to compile and run a Java program from the command line.
Java offers several ways to read console input. The best choice depends on the input format, the Java version, whether the program needs a real terminal, and how much control you need over errors.
Compare the main approaches
Scanner is usually the best default for small interactive programs. It can read words, numbers, and whole lines with simple methods. It also provides methods such as hasNextInt() for validation.
Console is designed for terminal interaction. It can display prompts and read passwords without echoing the password. However, System.console() can return null when the program runs in an IDE, a test runner, or with redirected input.
IO.readln is a compact helper available with Java 25 compact source files. It reads one line at a time. You convert the returned text yourself, so it is convenient for short examples but does not provide Scanner-style token validation.
BufferedReader reads text efficiently, one line at a time. It is useful for large input, precise line processing, or code that already uses the java.io APIs. It requires more exception handling.
| Approach | Best for | Main limitation |
|---|---|---|
| `Scanner` | Small programs with tokens and numbers | Mixing token and line reads requires care |
| `Console` | Terminal prompts and passwords | The console may be unavailable |
| `IO.readln` | Short Java 25 input examples | You parse and validate values yourself |
| `BufferedReader` | Efficient line-oriented input | More verbose exception handling |
A practical decision process
Use this sequence when choosing:
- Choose
Consolewhen the program needs a password or must behave like a terminal application. - Choose
IO.readlnwhen you are using Java 25 compact source files and each input value is a complete line. - Choose
Scannerwhen you need a simple interactive program that reads integers, decimals, words, or lines. - Choose
BufferedReaderwhen input is large, strictly line-based, or performance matters.
The diagram shows how the input requirements lead to a tool choice.
Match the API to the input source
All four approaches ultimately read from some input source. Interactive typing is only one possibility. A program might receive redirected text from a file or input from a pipe.
For example, this command sends two lines to a program:
printf "Ada\n42\n" | java GreeterA program that expects input to end must detect end of input. Scanner.hasNextLine() and BufferedReader.readLine() returning null are suitable for this case. A program that asks a human for a password should use Console instead, because redirected input is not a secure terminal prompt.
Avoid common design mistakes
Do not select an API only because it has the shortest code. Consider how errors should be handled. Scanner.nextInt() can throw InputMismatchException when the next token is not an integer. IO.readln returns text, so Integer.parseInt can throw NumberFormatException. BufferedReader can require handling IOException.
Also decide who owns System.in. Closing a Scanner created with System.in closes the underlying input stream. After that, another part of the program cannot read from standard input. For a small program, this is usually harmless. In a larger program, avoid closing the scanner until all input work is complete.
Choose deliberately
Scanner gives the most convenient balance for beginner and intermediate command-line programs. Console is the specialized choice for terminal security. IO.readln keeps Java 25 examples short. BufferedReader gives line-oriented control and good performance.
Exercise
Which API should a terminal login program use to read a username and a hidden password, and why?
Show answer
It should use java.io.Console. Its readPassword method avoids echoing the password, but the program must handle System.console() returning null.