Clean • Professional
This guide covers user input handling in Java using both Scanner and BufferedReader, including examples, best practices, and comparison.
In Java, you often need to read data from the user.
Two commonly used classes for this purpose are:
java.util)java.io)Both allow reading text input, but have different use cases and performance characteristics.
Scanner ClassScanner is easy to use and popular for console input.int, double, String, etc.Import Scanner
import java.util.Scanner;
Example: Reading Basic Inputs
import java.util.Scanner;
public class ScannerExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine(); // read a line
System.out.print("Enter your age: ");
int age = sc.nextInt(); // read an integer
System.out.println("Name: " + name);
System.out.println("Age: " + age);
sc.close(); // close scanner
}
}
Output (User Input Example):
Enter your name: John
Enter your age: 25
Name: John
Age: 25
Code Explanation
Scanner sc = new Scanner(System.in); → Creates a Scanner object to read input from the keyboard.nextLine() → Reads a full line (including spaces).nextInt() → Reads an integer input.System.out.println() → Displays the output to the console.sc.close() to release the input stream resource.Scanner Methods
| Method | Description |
|---|---|
nextInt() | Reads an integer |
nextDouble() | Reads a double |
nextLine() | Reads a whole line of text |
next() | Reads a single word (space-delimited) |
nextBoolean() | Reads a boolean value |
Tip: After nextInt() or nextDouble(), use sc.nextLine() if you plan to read a line afterward to avoid skipping input.
BufferedReader ClassIOException).Import BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
Example: Reading Input
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.print("Enter your age: ");
int age = Integer.parseInt(br.readLine()); // convert string to int
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Output (User Input Example):
Enter your name: Alice
Enter your age: 30
Name: Alice
Age: 30
Code Explanation :
BufferedReader reads input as text (String) only.
Integer.parseInt() or Double.parseDouble().InputStreamReader(System.in) connects standard input (keyboard) to the BufferedReader stream.IOException — so either handle with try-catch or declare throws IOException in the method signature.BufferedReader Methods
| Method | Description |
|---|---|
readLine() | Reads a line of text as String |
close() | Closes the reader |
| Feature | Scanner | BufferedReader |
|---|---|---|
| Performance | Slower | Faster |
| Ease of Use | Easy (parses int, double, etc.) | Slightly complex (needs parsing) |
| Exception Handling | Not needed | Must handle IOException |
| Best Use Case | Small programs, simple input | Large input, performance-sensitive programs |
import java.util.Scanner;
public class MultipleInputExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter 3 numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int sum = a + b + c;
System.out.println("Sum: " + sum);
sc.close();
}
}
Sample Input / Output:
Enter 3 numbers: 10 20 30
Sum: 60
Code Explanation
int sum = a + b + c; performs arithmetic operations using the entered values.Scanner or BufferedReader to avoid memory leaks.nextInt() with nextLine() in Scanner without proper handling.