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.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Geeks {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(
new InputStreamReader(System.in));
String s = r.readLine();
System.out.println(s);
}
}
Input:
Geek
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.
import java.util.Scanner;
class Geeks {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
String s1 = s.nextLine();
System.out.println("You entered string " + s1);
int a = s.nextInt();
System.out.println("You entered integer " + a);
float b = s.nextFloat();
System.out.println("You entered float " + b);
s.close();
}
}
Input:
GeeksforGeeks
12
3.4
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()).
import java.io.Console;
public class Geeks {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("Console not available");
return;
}
String s = console.readLine("Enter a string: ");
System.out.println("You entered string " + s);
}
}
Input:
GeeksforGeeksOutput:

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().
class Geeks {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("The command line arguments are:");
for (String val : args)
System.out.println(val);
} else {
System.out.println("No command line arguments found.");
}
}
}
Command Line Arguments:
javac Geeks.java
java Main Hello WorldOutput:

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.
import java.io.*;
public class Geeks {
public static void main(String[] args) throws IOException {
DataInputStream r = new DataInputStream(System.in);
System.out.print("Enter an integer: ");
int i = Integer.parseInt(r.readLine());
System.out.print("Enter a string: ");
String s = r.readLine();
System.out.println("You entered integer: " + i);
System.out.println("You entered string: " + s);
}
}
Input:
Enter an integer: 10
Enter a string: GeeksForGeeksOutput:
