Java Console reader() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

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()
CallWhat 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

ParameterDescription
Nonereader() takes no parameters.

Return Value

Return ValueDescription
ReaderThe single Reader associated with this console. Calling reader() again returns the same object.

Compare with Similar Methods

Other ways to get console input:

MethodHow it differs
readLine()Reads one line directly; simpler for most programs.
writer()The output twin: the console PrintWriter.
new Scanner(System.in)Reads standard input, which also works when input is redirected.
readPassword()Reads with echo turned off, for secrets.

Java Examples

Example 1 — Read Two Numbers with a Scanner

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);
    }
}

How It Works

  1. Scanner over the console. new Scanner(console.reader()) makes the Scanner pull its characters from the terminal keyboard.
  2. Visible prompt. console.printf flushes, so the prompt is on screen before nextInt() starts waiting.
  3. 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.
  4. 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());
        }
    }
}

How It Works

  1. Line reader on top. BufferedReader collects characters from console.reader() until the end of the line and returns them as one String.
  2. Checked exception. Unlike Console.readLine(), BufferedReader.readLine() declares IOException, so main adds throws IOException.
  3. Split at commas. split(",") cuts the typed line into three parts: "red", " green ", and "blue", spaces included.
  4. 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());
    }
}

How It Works

  1. Peek before reading. hasNextInt() looks at the next token without consuming it, so the loop runs only while a number is waiting.
  2. Running total. 80, 95, and 70 are added one by one, giving a count of 3 and a sum of 245.
  3. 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.
  4. 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.

Next: writer()

Get the console PrintWriter for print(), println(), and printf() style output.

writer() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful