The BufferedReader class is another way to read input in Java. While Scanner is usually the first choice for beginners, BufferedReader introduces an important idea: reading text efficiently through a character-based input stream.
What Is BufferedReader?
BufferedReader is a class from the java.io package that reads text from a character input stream. It is commonly used with InputStreamReader to read input from the keyboard.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = br.readLine();
System.out.println("Hello " + name);
}
}
There are several parts in this example, so let us understand the flow rather than memorizing the complete statement.
Why Do We Use BufferedReader?
BufferedReader is designed to read characters efficiently by using a buffer. Instead of requesting every character separately from the underlying input source, it can collect data into a buffer and read from that buffer.
For simple beginner programs, this difference may not be noticeable. However, BufferedReader is important because it teaches a more general Java I/O concept and is still encountered in existing applications, competitive programming, file processing, and technical interviews.
Do not think of BufferedReader as simply a faster version of Scanner. They are different classes with different APIs and design purposes.
Importing BufferedReader
BufferedReader belongs to the java.io package.
import java.io.BufferedReader;
We also need InputStreamReader because keyboard input from System.in is provided as a byte stream, while BufferedReader works with character streams.
import java.io.InputStreamReader;
Understanding the Input Chain
The following statement is one of the most important lines to understand when learning BufferedReader.
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
The flow can be understood from the inside outward.
- System.in represents the standard input stream.
- InputStreamReader converts the byte stream into a character stream.
- BufferedReader reads characters efficiently and provides convenient methods such as readLine().
- br is the reference used to access BufferedReader methods.
Remember the chain as: System.in → InputStreamReader → BufferedReader.
Reading a Line with readLine()
The most commonly used BufferedReader method for console input is readLine().
System.out.print("Enter your city: ");
String city = br.readLine();
System.out.println("City: " + city);
The readLine() method reads an entire line of text and returns it as a String.
For example, if the user enters Berhampur Odisha, the complete text can be stored in the String variable.
Reading Numbers with BufferedReader
Unlike Scanner, BufferedReader does not provide separate methods such as nextInt() or nextDouble(). The input is first received as text and then converted into the required data type.
System.out.print("Enter your age: ");
int age = Integer.parseInt(br.readLine());
System.out.println("Age: " + age);
Here, br.readLine() returns a String. The Integer.parseInt() method converts that String into an integer.
Reading a Double
The same idea can be used for decimal values.
System.out.print("Enter your percentage: ");
double percentage = Double.parseDouble(br.readLine());
System.out.println("Percentage: " + percentage);
The input is first read as a String and then converted into a double.
Reading Multiple Values
BufferedReader can be used to collect several values from the user.
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
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());
System.out.print("Enter your marks: ");
double marks = Double.parseDouble(br.readLine());
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Marks: " + marks);
}
}
Notice the difference from Scanner. Every value initially comes through readLine() as text. Numeric values must then be converted explicitly.
Scanner vs BufferedReader
Both classes can be used for console input, but their APIs work differently.
| Feature | Scanner | BufferedReader |
|---|---|---|
| Package | java.util | java.io |
| Basic text method | next() / nextLine() | readLine() |
| Integer input | nextInt() | Integer.parseInt(readLine()) |
| Double input | nextDouble() | Double.parseDouble(readLine()) |
| Ease of use | Very beginner-friendly | Requires explicit conversion for numbers |
| Primary idea | Convenient token-based input | Buffered character-based reading |
Why Does BufferedReader Need Exception Handling?
Reading input is an I/O operation, and Java requires checked exceptions associated with several I/O methods. That is why you may see throws Exception in simple examples.
public static void main(String[] args) throws Exception {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
System.out.println(name);
}
For beginner examples, throws Exception keeps the focus on understanding input. Later, when learning exception handling properly, you will learn how specific exceptions can be handled with try-catch.
Common Beginner Mistake
One common mistake is expecting BufferedReader to automatically convert input into numbers.
int age = br.readLine();
This does not work because readLine() returns a String, not an int.
The correct approach is to convert the returned String.
int age = Integer.parseInt(br.readLine());
The key difference to remember is simple: Scanner can directly provide typed input methods, while BufferedReader primarily reads text and leaves numeric conversion to you.
Interview Insight
A common interview question is: "What is the difference between Scanner and BufferedReader?" A strong beginner-level answer is that Scanner provides convenient methods for reading different data types, while BufferedReader is designed for efficient character-based reading and commonly reads complete lines through readLine().
Quick Revision
| Concept | Remember |
|---|---|
| BufferedReader | Reads characters efficiently from an input stream. |
| java.io | Package containing BufferedReader. |
| InputStreamReader | Connects byte-based input to character-based reading. |
| readLine() | Reads a complete line and returns a String. |
| Integer.parseInt() | Converts a String into an int. |
| Double.parseDouble() | Converts a String into a double. |
| Input flow | System.in → InputStreamReader → BufferedReader. |
BufferedReader gives you a deeper understanding of Java input because it makes the conversion process visible: data enters as text, and your program decides how that text should be interpreted. Once this becomes familiar, the next console topic becomes much easier to understand.
