Cosmic Module
J
Qubits of DPK
March 15, 2026
Core Java
Scanner — Simple Input
java
QUBITS OF DPK
Scanner Methods
java
QUBITS OF DPK
BufferedReader — Faster Input
java
QUBITS OF DPK
Scanner vs BufferedReader
️ Traps
java
QUBITS OF DPK
Interview Answer
"Scanner is the standard way to read user input — provides typed methods like nextInt(), nextDouble(), nextLine(). BufferedReader is faster for large input since it buffers reads, used in competitive programming. Key trap: nextInt() leaves a newline in the buffer so the subsequent nextLine() reads empty — fix by calling nextLine() once after nextInt() to flush the buffer."
Interview Questions & MAANG-Level Answers
Q1. What is the difference between Scanner and BufferedReader?
Scanner: from java.util, parses input with built-in methods (nextInt, nextDouble, nextLine), slower due to regex parsing, no checked exception. BufferedReader: from java.io, reads line by line as String (requires manual parseInt), much faster due to buffering, throws IOException (checked). Use Scanner for simple programs with mixed input types. Use BufferedReader in competitive programming or when reading large volumes of data where speed matters.
Q2. Why does nextInt() cause issues with the following nextLine()?
nextInt() reads the integer but leaves the \n (newline) character in the buffer. The subsequent nextLine() reads this leftover \n and returns an empty string immediately without waiting for user input. Fix: call scanner.nextLine() immediately after nextInt() to consume the leftover newline before reading actual string input. Same issue with nextDouble(), nextFloat(), nextBoolean() — any typed method leaves the newline.
Q3. Should you always close Scanner/BufferedReader?
Yes for file/network streams — not closing leaks resources. For System.in (keyboard input), closing Scanner also closes System.in which cannot be reopened — if your program needs to read more input later, this causes NoSuchElementException. Best practice: use try-with-resources for file-based readers. For System.in in short programs, closing is fine but be aware of the consequence. In production, always use try-with-resources for any Closeable resource.
Q4. How does BufferedReader improve performance?
BufferedReader wraps an underlying Reader and maintains an internal buffer (default 8KB). Instead of reading one character at a time from disk/network (slow OS syscall), it reads a chunk into buffer and serves subsequent reads from memory. Reading 1000 characters: without buffer = 1000 OS calls; with buffer = 1 OS call + 999 memory reads. readLine() reads until newline from this buffer — very efficient for line-by-line processing.
Q5. What is the difference between next() and nextLine() in Scanner?
next() reads the next token (word) — stops at whitespace (space, tab, newline). nextLine() reads the entire line including spaces — stops only at newline. Example: input "Hello World". scanner.next() returns "Hello" (stops at space). scanner.nextLine() returns "Hello World" (full line). Use next() for single words/tokens, nextLine() for full sentences or when input may contain spaces.