1. Introduction
The Scanner class, part of the java.util package, is the most common way to read input from the user via the console (or other input sources like files) in Java. It provides convenient methods to parse primitive types and strings from a stream of text.
Cricket analogy: The Scanner class is like a stadium's ball-tracking system reading live data off the pitch—runs, speed, trajectory—Scanner similarly reads input from the console or files and parses it into primitives like int and double or plain strings.
To use Scanner, you must import java.util.Scanner and create a Scanner object wrapping System.in, which represents standard input from the keyboard.
Cricket analogy: Using Scanner is like a commentator needing to first tune into the stadium's live feed (import java.util.Scanner) and then plug their headset into the broadcast desk representing System.in before any commentary can begin.
2. Syntax
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
int age = sc.nextInt(); // reads an integer
double price = sc.nextDouble(); // reads a double
String word = sc.next(); // reads a single word (no spaces)
String line = sc.nextLine(); // reads an entire line, including spaces
sc.close(); // close the scanner when done3. Explanation
Scanner offers dedicated methods for each primitive type: nextInt() for int, nextDouble() for double, nextLong() for long, nextBoolean() for boolean, and so on. The next() method reads a single token (word) up to the next whitespace, while nextLine() reads everything up to and including the newline character, allowing you to capture multi-word input like a full name or sentence.
Cricket analogy: Scanner's typed methods are like a scorer using separate columns for runs (nextInt), strike rate (nextDouble), and whether a wicket fell (nextBoolean); next() records just one word like 'SIX' while nextLine() captures the full commentary sentence including spaces, up to the newline.
It is good practice to call sc.close() once input reading is finished, to release the underlying resource, although for System.in this is often omitted in short programs since it also closes standard input for the rest of the JVM session.
Cricket analogy: Closing the Scanner is like a ground staff shutting the stadium gates after the match — good practice to free up the venue — but if it's the only ground in town (System.in), locking it early would block every future match for the rest of the season.
The classic nextInt()/nextLine() buffer trap: nextInt() reads only the numeric token and leaves the trailing newline character in the input buffer. If you call nextLine() immediately after nextInt(), it will read that leftover empty newline instead of waiting for new input, causing the next line of actual input to be skipped. The fix is to add an extra sc.nextLine() call right after nextInt() to consume the leftover newline, or use sc.nextLine() and parse it manually.
4. Example
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
sc.nextLine(); // consume leftover newline to avoid the buffer trap
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.println("Name: " + name + ", Age: " + age);
sc.close();
}
}5. Output
Enter your age: 25
Enter your full name: John Smith
Name: John Smith, Age: 256. Key Takeaways
- Scanner (java.util.Scanner) is used to read console input in Java.
- Create it with new Scanner(System.in), and always import java.util.Scanner.
- next() reads a single word; nextLine() reads the whole line including spaces.
- nextInt(), nextDouble(), nextLong(), nextBoolean() read specific primitive types.
- Mixing nextInt() and nextLine() causes the classic leftover-newline buffer trap.
- Call sc.close() when done reading input to release resources.
Practice what you learned
1. Which import statement is required to use the Scanner class?
2. What is the classic bug that occurs when nextLine() is called immediately after nextInt()?
3. Which Scanner method reads an entire line of text, including spaces?
4. How do you create a Scanner object to read from the console?
5. What is the recommended fix for the nextInt()/nextLine() buffer issue?
Was this page helpful?
You May Also Like
Data Types in Java
A complete guide to Java's 8 primitive data types with exact sizes and ranges, plus reference types like String and arrays.
Comments in Java
Learn the three types of comments in Java — single-line, multi-line, and Javadoc — and best practices for using them.
Strings in Java
Understand Java's String class, its immutability, the string constant pool, and why == should never be used to compare string content.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics