Read User Input from Console in Java: 3 Effective Methods

In Java, there are three different ways to read input from a user in a command-line environment (console).

Use the buffer reader class

This is the Java Classic input method introduced in JDK 1.0. Using this method by wrapping the System.in (standard input stream) in an InputStreamReader (wrapped in a BufferedReader), we can read the input from the user on the command line.

The input is buffered so that it can be read efficiently.

Packaging codes are hard to remember.

Procedure:

// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test 
{
     public static void main(String[] args) throws IOException 
     {
         //Enter data using BufferReader
         BufferedReader reader = 
                    new BufferedReader( new InputStreamReader(System.in));
         
         // Reading data using readLine
         String name = reader.readLine();
  
         // Printing the read line
         System.out.println(name);        
     }
}

Enter the following:

Geek

The output is as follows:

Geek

Note: To read other types, we use functions like Integer.parseInt(), Double.parseDouble(). To read multiple values, we use split().

Use the Scanner class

This is probably the most preferred input method. The main purpose of the Scanner class is to parse primitive types and strings using regular expressions, but it can also be used to read input from the user on the command line.

Merit:

A convenient way to parse primitives (nextInt(), nextFloat(), etc.) from tokenized inputs.

Regular expressions can be used to find tags.

Shortcoming:

The read methods are out of sync

To see more differences, see this article.

// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
  
class GetInputFromUser
{
     public static void main(String args[])
     {
         // Using Scanner for Getting Input from User
         Scanner in = new Scanner(System.in);
  
         String s = in.nextLine();
         System.out.println( "You entered string " +s);
  
         int a = in.nextInt();
         System.out.println( "You entered integer " +a);
  
         float b = in.nextFloat();
         System.out.println( "You entered float " +b);
     }
}

Enter the following:

lsbin
12
3.4

The output is as follows:

You entered string lsbin
You entered integer 12
You entered float 3.4

Use the console class

It has become the preferred method for reading user input from the command line. In addition, it can be used to read password-like inputs without having to echo the characters entered by the user; You can also use formatted string syntax (e.g. System.out.printf()).

Merit:

  • Read the password without echoing the characters entered.
  • The read method is synchronous.
  • You can use format string syntax.

Shortcoming:

Does not work in non-interactive environments such as in IDEs.

// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as 
// System.console() may require console
public class Sample 
{
     public static void main(String[] args) 
     {        
         // Using Console to input data from user
         String name = System.console().readLine();
          
         System.out.println(name);
     }
}

Please refer to this to read the input at a faster speed.

If you find anything incorrect, or would like to share more information about the above topics, please write a comment.