VOOZH about

URL: https://www.geeksforgeeks.org/java/ways-to-read-input-from-console-in-java/

⇱ Ways to Read Input from Console in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ways to Read Input from Console in Java

Last Updated : 16 Jan, 2026

Reading input from the console is a common requirement for building interactive programs. Java provides multiple ways to read user input in a command-line (console) environment. Each approach has its own use cases, advantages, and limitations depending on performance, simplicity, and environment.

Below are the commonly used ways to read input from the console in Java.

1. Using the Buffered Reader Class

The BufferedReader class is the classical method to take input, introduced in JDK 1.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. 

  • Input is buffered, making it faster for large inputs.
  • Suitable for competitive programming and performance-critical applications.
  • Slightly verbose and harder to remember compared to Scanner.

Input:

Geek

Output:

πŸ‘ Output

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

2. Using Scanner Class

Scanner Class is probably the most preferred method to take input, Introduced in JDK 1.5. 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. 

  • Easy to use and understand.
  • Provides built-in methods like nextInt(), nextFloat(), etc.
  • Slightly slower than BufferedReader due to parsing overhead.

Input:

GeeksforGeeks
12
3.4

Output:

πŸ‘ Output

3. Using Console Class

Console Class has been becoming a preferred way for reading user’s input from the command line, Introduced in JDK 1.6. 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()). 

Input:

GeeksforGeeks

Output:

πŸ‘ Output

Advantages:

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

Limitations: Does not work in non-interactive environment (such as in an IDE).

4. Using Command line argument

Command line argument available since JDK 1.0, are used to pass inputs to a Java program at runtime, commonly in competitive coding. The inputs are stored as strings in the args[] array and can be converted to numeric values using methods like Integer.parseInt() or Float.parseFloat().

Command Line Arguments:

javac Geeks.java
java Main Hello World

Output:

πŸ‘ Output

5. Using DataInputStream Class

DataInputStream class in Java (introduced in JDK 1.0) is used to read primitive data types from an input stream in a machine-independent format. It belongs to the java.io package and wraps an existing input stream, commonly used with DataOutputStream.

Input:

Enter an integer: 10
Enter a string: GeeksForGeeks

Output:

πŸ‘ Output

Please refer this for more faster ways of reading input.

Comment
Article Tags:
Article Tags: