Ways to read input from console in Java

1.8

In Java, there are three different ways for reading input from the user in the command line environment(console).

1.Using Buffered Reader Class

This is the Java classical method to take input, Introduced in JDK1.0. This method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader, we can read input from the user in the command line.

    Advantages
  • The input is buffered for efficient reading.
    Drawback:
  • The wrapping code is hard to remember.

Program:

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

Input:

Geek

Output:

Geek

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

2. Using Scanner Class

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

    Advantages:

  • Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
  • Regular expressions can be used to find tokens.
    Drawback:

  • The reading methods are not synchronized

To see more differences, please 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);
    }
}

Input:

GeeksforGeeks
12
3.4

Output:

You entered integer 12
Enter a float
You entered float 3.4

3. Using Console Class

It has been becoming a preferred way for reading user’s input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also be used (like System.out.printf()).
Advantages:

  • Reading password without echoing the entered characters.
  • Reading methods are synchronized.
  • Format string syntax can be used.
    Drawback:

  • Does not work in non-interactive environment (such as in an IDE).
// 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 this for more faster ways of reading input.

This article is contributed by D Raj Ranu. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



1.8 Average Difficulty : 1.8/5.0
Based on 13 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.