100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Scanner Class in Java

Learn how to read user input in Java using the Scanner class, including nextInt(), nextLine(), nextDouble(), and the classic buffer trap.

Basics & Data TypesBeginner7 min readJul 7, 2026
Analogies

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

java
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 done

3. 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

java
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

text
Enter your age: 25
Enter your full name: John Smith
Name: John Smith, Age: 25

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ScannerClassInJava#Scanner#Class#Syntax#Explanation#OOP#StudyNotes#SkillVeris