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 java.io 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 System.in (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(System.in));
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, System.in 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.in);

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 java.io.Console way. It simply makes code more readable. What is your choice to read the test from Console in Java.

Happy Learning !!

Comments

Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

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

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

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode