Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Mastering Java Scanner: A Comprehensive Guide with Practical Examples

Mastering Java Scanner

Discover how to use Java’s Scanner class to read text, numbers, and complex types from various sources, backed by clear, real‑world examples.

The Scanner class in java.util lets you capture input from streams, files, strings, and the console. Below we walk through its core usage patterns.


Example 1: Reading a Full Line of Text

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    // Create a Scanner that reads from standard input
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your name: ");
    // Read an entire line, including spaces
    String name = input.nextLine();
    System.out.println("My name is " + name);
    input.close();
  }
}

Output

Enter your name: Kelvin
My name is Kelvin

Notice the line Scanner input = new Scanner(System.in); – it constructs a Scanner that listens to keyboard input.

The nextLine() method consumes the whole line until a line break.


Importing the Scanner Class

Before you can use Scanner, import it:

import java.util.Scanner;

For more on package imports, see Java Packages.


Creating Scanner Objects

Depending on your input source, instantiate a Scanner like this:

// From an input stream (e.g., System.in)
Scanner sc1 = new Scanner(InputStream input);

// From a file
Scanner sc2 = new Scanner(File file);

// From a string
Scanner sc3 = new Scanner(String str);

Key Scanner Methods for Input

The Scanner class offers a variety of methods to read different data types. Below is a quick reference:

MethodDescription
nextInt()Read an int
nextFloat()Read a float
nextBoolean()Read a boolean
nextLine()Read a full line of text
next()Read a single word (up to whitespace)
nextByte()Read a byte
nextDouble()Read a double
nextShort()Read a short
nextLong()Read a long

Example 2: Using nextInt()

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter an integer: ");
    int data1 = input.nextInt();
    System.out.println("Using nextInt(): " + data1);
    input.close();
  }
}

Output

Enter an integer:
22
Using nextInt(): 22

Example 3: Using nextDouble()

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter Double value: ");
    double value = input.nextDouble();
    System.out.println("Using nextDouble(): " + value);
    input.close();
  }
}

Output

Enter Double value: 33.33
Using nextDouble(): 33.33

Example 4: Using next()

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String value = input.next();
    System.out.println("Using next(): " + value);
    input.close();
  }
}

Output

Enter your name: Jonny Walker
Using next(): Jonny

Notice that next() stops at the first whitespace, so only the first name is captured.


Example 5: Using nextLine()

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String value = input.nextLine();
    System.out.println("Using nextLine(): " + value);
    input.close();
  }
}

Output

Enter your name: Jonny Walker
Using nextLine(): Jonny Walker

Unlike next(), nextLine() captures the entire line, including spaces, until a newline character.

Recommended Reading: Java Scanner skipping the nextLine()


Scanner with BigInteger and BigDecimal

For high‑precision numbers, Scanner supports:

Example: Reading BigInteger and BigDecimal

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a big integer: ");
    BigInteger value1 = input.nextBigInteger();
    System.out.println("Using nextBigInteger(): " + value1);
    System.out.print("Enter a big decimal: ");
    BigDecimal value2 = input.nextBigDecimal();
    System.out.println("Using nextBigDecimal(): " + value2);
    input.close();
  }
}

Output

Enter a big integer: 987654321
Using nextBigInteger(): 987654321
Enter a big decimal: 9.55555
Using nextBigDecimal(): 9.55555

How Scanner Works Internally

Scanner reads an entire line, then splits it into tokens using whitespace by default. Each token can then be retrieved with the appropriate method. For example, given the input He is 22, the tokens are He, is, and 22.

Tip: Tokens are separated by spaces, tabs, or newlines unless you customize the delimiter.


Java

  1. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  2. Java InputStream Class – Understanding Core Methods & Practical Example
  3. Mastering Java FileInputStream: A Practical Guide with Code Examples
  4. Java ByteArrayInputStream: A Practical Guide to Reading Byte Arrays
  5. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  6. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  7. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  8. Mastering Java Reader Class: Subclasses, Methods, and Practical Example
  9. Java InputStreamReader Explained: Methods, Encoding, and Practical Examples
  10. Mastering Java Generics – Building Reusable, Type‑Safe Code