In Java, console.reader() returns the java.io.Reader connected to the console's keyboard input. Every call returns the same object, because a console has exactly one reader.
A bare Reader reads characters, which is low level. Its real use is as a source for richer readers: new Scanner(console.reader()) splits input into numbers and words, and new BufferedReader(console.reader()) reads whole lines. For simple line input, readLine() is easier.
An instance method of the java.io.Console class, available since Java 6. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.reader(...).
Syntax
Get the reader and hand it to the class that does the parsing:
Java
Console console = System.console();
Reader r = console.reader(); // the console input
Scanner sc = new Scanner(console.reader()); // tokens: nextInt(), next()
BufferedReader br = new BufferedReader(console.reader()); // lines: readLine()
Call
What it does
console.reader()
Returns the unique Reader of the console.
new Scanner(console.reader())
Parses numbers and words typed in the terminal.
new BufferedReader(console.reader())
Reads lines; its readLine() throws IOException.
console.reader().read(buffer)
Fills a char array, but never past the end of the current line.
Available since Java 6. The bulk read(char[]) methods of this reader never read past a line bound (a line feed, a carriage return, or the end of input) in one call, even if the buffer has room, and they may block until the user presses Enter.
Library declarations
Java
// java.io.Console
public Reader reader()
Parameters
Parameter
Description
None
reader() takes no parameters.
Return Value
Return Value
Description
Reader
The single Reader associated with this console. Calling reader() again returns the same object.
Wrap console.reader() in a Scanner to read whole numbers directly, without parsing strings yourself.
Java
import java.io.Console;
import java.util.Scanner;
public class ConsoleReaderScanner {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
Scanner in = new Scanner(console.reader());
console.printf("Enter width and height: ");
int width = in.nextInt();
int height = in.nextInt();
console.printf("Area of %d x %d = %d%n", width, height, width * height);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleReaderScanner. (user types ...) marks keyboard input, not program output. The last part is a run where System.console() is null (IDE, redirected input).
Enter width and height: 7 6 (user types: 7 6)
Area of 7 x 6 = 42
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
Scanner over the console.new Scanner(console.reader()) makes the Scanner pull its characters from the terminal keyboard.
Visible prompt.console.printf flushes, so the prompt is on screen before nextInt() starts waiting.
Tokens, not lines. The user types 7 6 on one line; the first nextInt() takes 7 and the second takes 6, because spaces separate tokens.
Not closed. The Scanner is left open on purpose, since the console reader is shared with the rest of the program.
Example 2 — Read a Comma-Separated Line with BufferedReader
BufferedReader reads whole lines from the console reader. Its readLine() throws the checked IOException, so main declares it.
Java
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
public class ConsoleReaderBuffered {
public static void main(String[] args) throws IOException {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
BufferedReader in = new BufferedReader(console.reader());
console.printf("Enter colors separated by commas: ");
String line = in.readLine();
if (line == null) {
console.printf("No input.%n");
return;
}
String[] colors = line.split(",");
for (int i = 0; i < colors.length; i++) {
console.printf("%d. %s%n", i + 1, colors[i].trim());
}
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleReaderBuffered. (user types ...) marks keyboard input, not program output.
Enter colors separated by commas: red, green ,blue (user types: red, green ,blue)
1. red
2. green
3. blue
How It Works
Line reader on top.BufferedReader collects characters from console.reader() until the end of the line and returns them as one String.
Checked exception. Unlike Console.readLine(), BufferedReader.readLine() declares IOException, so main adds throws IOException.
Split at commas.split(",") cuts the typed line into three parts: "red", " green ", and "blue", spaces included.
Tidy numbering.trim() strips the extra spaces and i + 1 numbers the colors from 1.
Example 3 — Read Scores Until a Non-Number Appears
hasNextInt() lets a Scanner on the console keep reading numbers until the next word is not a number. The last line checks that reader() always returns one object.
Java
import java.io.Console;
import java.util.Scanner;
public class ConsoleReaderScores {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
Scanner in = new Scanner(console.reader());
console.printf("Enter scores, then type end: ");
int sum = 0;
int count = 0;
while (in.hasNextInt()) {
sum += in.nextInt();
count++;
}
String stop = in.hasNext() ? in.next() : "(end of input)";
console.printf("%d scores, sum %d, stopped at %s%n", count, sum, stop);
console.printf("Same Reader every call? %b%n", console.reader() == console.reader());
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleReaderScores. (user types ...) marks keyboard input, not program output.
Enter scores, then type end: 80 95 70 end (user types: 80 95 70 end)
3 scores, sum 245, stopped at end
Same Reader every call? true
How It Works
Peek before reading.hasNextInt() looks at the next token without consuming it, so the loop runs only while a number is waiting.
Running total. 80, 95, and 70 are added one by one, giving a count of 3 and a sum of 245.
The stop word. "end" is not a number, so the loop exits and next() reads that word; at end of input the code prints a placeholder instead.
One reader per console. Two separate console.reader() calls return the identical object, so the comparison prints true.
Frequently Asked Questions
The unique java.io.Reader connected to the console input. Every call returns the same object.
Check that System.console() is not null, then create new Scanner(console.reader()) and call nextInt(), next(), or nextLine() on it.
readLine() reads one whole line and returns it as a String. reader() gives you the underlying Reader so that classes such as Scanner or BufferedReader can do the reading.
No. Only readPassword() turns echo off. Text read through reader() is visible on screen as it is typed.
The console reader stops at each line bound, so a single call never reads past the end of the current line, even when the buffer has room for more.