In Java, Scanner is a utility class used to read input (like numbers and text) from different sources such as the keyboard, files, and strings.

What is Scanner in Java?

Scanner is a class in the java.util package that lets your program accept and parse input data easily.

You can use it to read:

  • Keyboard input (System.in)
  • File input (new Scanner(new File("data.txt")))
  • String input (new Scanner("Hello 123"))

It can read different data types like:

  • String (words and full lines)
  • int, double, long, boolean, etc.
  • Even BigInteger and BigDecimal for very large numbers.

How Scanner Works (Intuition)

Think of Scanner as a reader that looks at a stream of characters and splits it into small meaningful chunks called tokens.

Example input:

He is 22

Scanner will see this as tokens: "He", "is", "22" (by default it splits on whitespace).

Then methods like next(), nextInt(), nextDouble() pick the next token and convert it to the right type.

Basic Syntax and Example

1. Importing Scanner

java

import java.util.Scanner;

2. Creating a Scanner for Keyboard Input

java

Scanner sc = new Scanner(System.in);

3. Reading Different Types

java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);  // create scanner object

        System.out.print("Enter your name: ");
        String name = sc.nextLine();          // reads a full line of text[web:3][web:6][web:9]

        System.out.print("Enter your age: ");
        int age = sc.nextInt();               // reads an int[web:1][web:2][web:3][web:7]

        System.out.println("Hello " + name + ", you are " + age + " years old.");
        sc.close();                           // close scanner when done[web:3][web:5][web:7]
    }
}

This pattern (create → prompt → read → use → close) is how Scanner is typically used in small console applications.

Common Scanner Methods

Here are some of the most used methods and what they do:

[5][7][9][3] [6][9][3] [1][7][2][3] [1][2][3] [2][3] [2][3] [2][3] [3] [3] [7][8][3]
Method What it reads Example use
next() Next word (stops at space) String word = sc.next();
nextLine() Whole line including spaces String line = sc.nextLine();
nextInt() Integer int n = sc.nextInt();
nextDouble() Double (decimal number) double d = sc.nextDouble();
nextBoolean() true or false boolean b = sc.nextBoolean();
nextLong() Long integer long l = sc.nextLong();
nextShort() Short value short s = sc.nextShort();
nextBigInteger() Big integer BigInteger bi = sc.nextBigInteger();
nextBigDecimal() Big decimal number BigDecimal bd = sc.nextBigDecimal();
hasNext(), hasNextInt(), etc. Check if another token of that type exists while (sc.hasNextInt()) { ... }

Mini Story: Scanner in a Job Application Console App

Imagine you are building a small console-based job application form.

You:

  1. Greet the user and ask for their full name using nextLine() so you can include spaces.
  1. Ask for age with nextInt(), salary expectation with nextDouble(), years of experience with nextInt(), relocation preference with nextBoolean(), etc.
  1. Then print back a neat summary of what they entered.

A simplified example based on such a scenario:

java

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your full name: ");
String name = scanner.nextLine();          // full name[web:2][web:3][web:6]

System.out.print("Enter your age: ");
int age = scanner.nextInt();               // age[web:2][web:3]

System.out.print("Expected salary: ");
double salary = scanner.nextDouble();      // salary[web:2][web:3]

System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Expected salary: " + salary);

scanner.close();

This is exactly the kind of beginner-friendly use case where Scanner shines in tutorials and forums today.

Scanner in Files and Strings

You’re not limited to keyboard input. Scanner can scan:

  • A file: new Scanner(new File("input.txt"))
  • A string: new Scanner("Hello 123 45.6")

Example reading a file line by line:

java

import java.io.File;
import java.util.Scanner;

File file = new File("data.txt");
Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {            // checks if more lines exist[web:3][web:7]
    String line = scanner.nextLine();      // read a line[web:3][web:7]
    System.out.println(line);
}

scanner.close();

Common Gotchas and Forum Talk

On forums (like r/javahelp), beginners often ask “What exactly is Scanner?” or “Why is my Scanner throwing errors?”.

Typical issues:

  • MixingnextInt() and nextLine(): After nextInt(), there’s a leftover newline, so the next nextLine() may read an empty string unless you consume that newline.
  • Using Scanner after closing it : Once you close a Scanner, using it again or reading from the same underlying stream can cause IllegalStateException.
  • Bad input : If a user types text where a number is expected, methods like nextInt() can throw InputMismatchException.

Even though newer input patterns and libraries exist, Scanner remains a trending teaching tool in 2025–2026 for basic console input because of its simplicity and readable syntax.

SEO-style Meta (for “what is scanner in java”)

  • Focus keyword : “what is scanner in java” – Scanner is a class in java.util used for parsing input from streams, files, and strings using token-based methods like nextInt() and nextLine().
  • Meta description : “Learn what Scanner is in Java, how it reads user input (int, double, String, etc.), how it tokenizes data, and see practical examples for console and file input.”

TL;DR

Scanner in Java is a built‑in input parser from java.util that reads and converts user or file input into useful data types like strings and numbers, using simple methods such as nextInt() and nextLine().

Information gathered from public forums or data available on the internet and portrayed here.