Input / Output in Java using Scanner and BufferedReader
This guide covers user input handling in Java using both Scanner and BufferedReader, including examples, best practices, and comparison.
Introduction to Input in Java
In Java, you often need to read data from the user.
Two commonly used classes for this purpose are:
- Scanner (part of
java.util) - BufferedReader (part of
java.io)
Both allow reading text input, but have different use cases and performance characteristics.
1. Using Scanner Class
- Introduced in Java 5,
Scanneris easy to use and popular for console input. - Supports reading different types:
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.- Always call
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.
2. Using BufferedReader Class
- Older, but faster than Scanner.
- Requires exception handling (
IOException). - Reads input as String, then you convert to other types.
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 :
-
BufferedReaderreads input as text (String) only. - You must convert the input manually using methods like
Integer.parseInt()orDouble.parseDouble(). InputStreamReader(System.in)connects standard input (keyboard) to the BufferedReader stream.- Throws
IOException— so either handle withtry-catchor declarethrows IOExceptionin the method signature.
BufferedReader Methods
| Method | Description |
|---|---|
readLine() | Reads a line of text as String |
close() | Closes the reader |
3. Scanner vs BufferedReader
| 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 |
4. Example – Reading Multiple Inputs
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
- You can enter multiple values separated by spaces — Scanner automatically separates them using whitespace.
int sum = a + b + c;performs arithmetic operations using the entered values.- This technique is commonly used in coding interviews or competitive programming for fast numeric input.
Best Practices
- Always close resources like
ScannerorBufferedReaderto avoid memory leaks. - Use BufferedReader for large or high-performance input scenarios.
- Prefer Scanner for simple console-based programs and interviews.
- Avoid mixing
nextInt()withnextLine()in Scanner without proper handling. - Always handle exceptions when using BufferedReader.
