Reading User Input from Console in Java

In this Java tutorial, we will learn how to read the user input text from the console in Java. Reading console input in programs may be necessary to make applications interactive.

1. Using Console

Console class is from package and is used to read from and write to the character-based console.

The System.console() is used to get the reference of the system console. Note that if JVM has been launched with a background job then the program will not have a console. In this case, invocation of System.console() method will return null.

  • The readLine() reads a single line of text from the console.
  • The readLine(line) writes the line into the console and then reads the user input from the console.
  • The readPassword() is used to read the secure input. For example, passwords and encryption keys.
  • The readPassword(line) prompts the line into the console and reads the secure user input. For example, passwords and encryption keys.
  • Passing a null argument to any method in this class will cause a NullPointerException to be thrown.
Console console = System.console();

String inputString = console.readLine("Enter Your Name: ");

System.out.println("The name entered: " + inputString);

The program output:

Enter Your Name: Lokesh
The name entered: Lokesh

2. Using BufferedReader

BufferedReader is supported since Java 1.1. We may see its usage in legacy Java applications. To read console input, we shall wrap the (standard input stream) in an InputStreamReader which again wrapped in a BufferedReader class.

BufferedReader reads text from the console, buffering characters so as to provide for the efficient reading of user input. It makes the read operations from InputStreamReader – less costly.

System.out.print("Enter Your Name: ");  //Prompt

BufferedReader bufferRead = new BufferedReader(new InputStreamReader(;
String inputString = bufferRead.readLine();
System.out.println("The name entered: " + inputString);

The program output:

Enter Your Name: Lokesh
The name entered: Lokesh

3. Using Scanner

In Java, represents the standard input. By default, it is the system console.

The Scanner class, when reading from the console, provides methods to read different types of data e.g. integers, numbers, strings, etc.

Scanner scanner = new Scanner(;

System.out.println("Enter name, age and salary:");

String name = scanner.nextLine();
int age = scanner.nextInt();
double salary = scanner.nextDouble();

System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);

Above all techniques are equally effective, but I personally like the way. It simply makes code more readable. What is your choice to read the test from Console in Java.

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

3 thoughts on “Reading User Input from Console in Java”

  1. Hi Lokesh,Thanks for sharing this post.

    for me usingConsoleReader() function not working.

    Console console=null;
    console = System.console();
    System.out.println(console); //always printing null for me. so further part not executing.

    can you tell me what will be the cause here?

    rest two function working properly.

    • The evaluation of System.console() depends entirely on the environment you run application in it. As per my knowledge, both Eclipse and Netbeans will cause System.console() to return null whereas running your application on the same machine’s command line shall return a valid Console object.

      • yeah exactly I was trying to run from My Eclipse……so I tested again from command line ,it is working fine…thanks again


Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.