Scanner vs BufferedReader
There are two kinds of ticket counters. One clerk reads your form, checks every field and fills in the details for you: helpful, but slow when the queue is long. The other just takes your form and passes it on: fast, but you have to fill everything in correctly yourself. Scanner is the helpful clerk. BufferedReader is the fast one. Both read input; they make different trade-offs.
Two Pipelines
The Same Task, Both Ways
Read a name on one line and an age on the next.
import java.util.Scanner;
public class WithScanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int age = sc.nextInt();
System.out.println(name + " is " + age);
}
}import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class WithReader {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
String name = in.readLine();
int age = Integer.parseInt(in.readLine().trim());
System.out.println(name + " is " + age);
}
}Srihitha Anamaneni is 25How it works
- Scanner reads straight from
System.in.nextInt()finds the number and converts it for you. - BufferedReader needs an
InputStreamReaderin between, which turns raw bytes into characters. ThenreadLine()returns a whole line as aString, and you convert it withInteger.parseInt. readLine()can throw anIOException, which Java makes you deal with. Here,maindeclaresthrows IOException. A later lesson explains exceptions..trim()removes stray spaces around the number, which would otherwise makeparseIntfail.
Try it yourself: in the BufferedReader version, type 25 with spaces around it, and then remove .trim(). What changes?
Side by Side
Scanner | BufferedReader | |
|---|---|---|
| Package | java.util | java.io |
| Reads | Tokens and lines | Whole lines (or single characters) |
Converts to int, double | Yes, nextInt() and friends | No; use Integer.parseInt yourself |
| Speed on large input | Slower: checks every token with patterns | Faster: big buffer, no parsing |
| Checked exception | No | Yes: IOException |
| Safe to share between threads | No | Yes |
| Typical use | Learning, small programs, interactive prompts | Large input files, competitive programming |
Why BufferedReader Is Faster
Both read from the keyboard or a file in chunks (a buffer) rather than one character at a time. The difference is what happens next. Scanner examines the text with regular expressions to find each token and decide whether it looks like a number. BufferedReader just hands over the whole line. For a few values, you'll never notice; for a million numbers in a coding contest, Scanner can be several times slower.
Common Mistakes
Forgetting throws IOException with BufferedReader. The compiler stops with unreported exception IOException; must be caught or declared to be thrown.
Calling parseInt on text with spaces or a blank line. Integer.parseInt(" 25") throws NumberFormatException. Trim first.
Choosing BufferedReader for a three-question quiz. The extra code buys nothing; Scanner is clearer.
Interview Corner
What is the difference between Scanner and BufferedReader?
Scanner (java.util) parses input into tokens and converts them to types with methods like nextInt(), but it's slower because it uses regular expressions. BufferedReader (java.io) reads whole lines quickly from a large buffer, leaves conversion to you, throws the checked IOException, and is thread-safe. Use Scanner for convenience and BufferedReader for large input.
Practice
- Easy: Rewrite the
Greetingprogram from the Scanner lesson usingBufferedReader. - Harder: Read a line such as
12 7 30withBufferedReader, split it withsplit(" "), and print the sum of the numbers.
Key Takeaways
Was this tutorial helpful?