Getty Images/iStockphoto

How Java's reads input from the user

The Java component provides universal access to console-based user input. Learn how to make your applications interactive with this Java tutorial.

What is

In Java, is the standard, universally accessible InputStream that developers use to read input from the terminal window.

However, Java's does not work alone. To read user input with, you must pass it as an argument to either:

  1. The constructor of the Scanner class
  2. The constructor of an InputStreamReader and Java's Scanner class

The easiest way to read input from the user with is to use the Scanner class.

The following code prompts the user for their name, consumes keyboard input and prints out a response to the client:

System.out.println("What is your name?");

Scanner scanner = new Scanner(;

String name = scanner.nextLine();

System.out.println(name + " is a nice name!"); and Java's BufferedStreamReader

Without the Scanner class, reading user input with is more complicated, and instead uses the InputStreamReader to convert data from bytes to characters. To do this, you must pass Java's to the constructor of an InputStreamReader, and then pass that to the BufferedReader.

The code below shows how to get user input with, the InputStreamReader and the BufferedReader:

InputStreamReader reader = new InputStreamReader(;

BufferedReader br = new BufferedReader(reader);

System.out.println("Whis is our name?");

var input = br.readline();

System.out.println("Your input was: " + input);

Regardless of whether you go the path of the Scanner, or combine the BufferedReader with the InputStreamReader, Java's InputStream is always at the heart of console-based input in the JVM.

Dig Deeper on Core Java APIs and programming techniques

App Architecture
Software Quality
Cloud Computing