Scanner vs BufferedReader

Naveen AnamaneniUpdated · 4 min read

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

Scanner converts for you; BufferedReader gives you whole lines

The Same Task, Both Ways

Read a name on one line and an age on the next.

WithScanner.java▶ Try it
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);
    }
}
Output
Srihitha Anamaneni is 25

How it works

  1. Scanner reads straight from System.in. nextInt() finds the number and converts it for you.
  2. BufferedReader needs an InputStreamReader in between, which turns raw bytes into characters. Then readLine() returns a whole line as a String, and you convert it with Integer.parseInt.
  3. readLine() can throw an IOException, which Java makes you deal with. Here, main declares throws IOException. A later lesson explains exceptions.
  4. .trim() removes stray spaces around the number, which would otherwise make parseInt fail.

Try it yourself: in the BufferedReader version, type 25 with spaces around it, and then remove .trim(). What changes?

Side by Side

ScannerBufferedReader
Packagejava.utiljava.io
ReadsTokens and linesWhole lines (or single characters)
Converts to int, doubleYes, nextInt() and friendsNo; use Integer.parseInt yourself
Speed on large inputSlower: checks every token with patternsFaster: big buffer, no parsing
Checked exceptionNoYes: IOException
Safe to share between threadsNoYes
Typical useLearning, small programs, interactive promptsLarge 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

  1. Easy: Rewrite the Greeting program from the Scanner lesson using BufferedReader.
  2. Harder: Read a line such as 12 7 30 with BufferedReader, split it with split(" "), and print the sum of the numbers.

Quick check: Scanner vs BufferedReader

4 questions
1Which one converts input to int for you?
2Why does BufferedReader need an InputStreamReader?
3Which is usually faster for very large input?
4What must main declare when it uses readLine() without catching errors?

Key Takeaways

Was this tutorial helpful?

Lesson 50 of 482