Cosmic Module

J

Qubits of DPK

March 15, 2026

Core Java

Scanner — Simple Input

java
QUBITS OF DPK
1import java.util.Scanner;
2
3Scanner scanner = new Scanner(System.in);
4
5System.out.print("Enter name: ");
6String name = scanner.nextLine();
7
8System.out.print("Enter age: ");
9int age = scanner.nextInt();
10
11System.out.print("Enter salary: ");
12double salary = scanner.nextDouble();
13
14System.out.println(name + ", " + age + ", " + salary);
15scanner.close();   // always close!

Scanner Methods

java
QUBITS OF DPK
1scanner.next()        // next token (no spaces)
2scanner.nextLine()    // full line including spaces
3scanner.nextInt()     // integer
4scanner.nextDouble()  // double
5scanner.nextBoolean() // boolean

BufferedReader — Faster Input

java
QUBITS OF DPK
1import java.io.*;
2
3BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
4
5String name = br.readLine();
6int age = Integer.parseInt(br.readLine());
7
8br.close();

Scanner vs BufferedReader

️ Traps

java
QUBITS OF DPK
1// Trap 1 — nextInt() leaves newline in buffer
2int age = scanner.nextInt();
3String name = scanner.nextLine();   // ⚠️ reads empty string!
4// Fix: add scanner.nextLine() after nextInt() to consume newline
5int age = scanner.nextInt();
6scanner.nextLine();   // consume leftover newline
7String name = scanner.nextLine();   // ✅ now reads correctly

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.