---
title: "Overview of Java Scanner Class"  
description: "The Scanner class in Java is part of the java.util package and provides a convenient way to parse primitive types and strings using regular expression"  
author: "Ravi Vishwakarma"  
published: 2024-06-02  
updated: 2024-06-02  
canonical: https://www.mindstick.com/blog/304297/overview-of-java-scanner-class  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# Overview of Java Scanner Class

The **Scanner** class in Java is part of the **java.util** package and provides a convenient way to parse primitive types and strings using [regular expressions](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table). It is commonly used for [reading input](https://www.mindstick.com/forum/158952/what-are-the-different-methods-for-reading-input-from-the-user-in-python) from various sources, including standard input, files, and strings.

####

#### Overview

- **Package**: **java.util**
- **Introduced in**: Java 5 (JDK 1.5)
- **Purpose**: To simplify input parsing.

####

#### Importing the Scanner Class

To use the **Scanner** class, you need to import it:

```java
import java.util.Scanner;
```

#### Creating a Scanner Object

You can create a **Scanner** object to read input from different sources:

**Standard Input (Console)**:

```java
Scanner scanner = new Scanner(System.in);
```

**File Input**:

```java
import java.io.File;
import java.io.FileNotFoundException;

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

**String Input**:

```java
String input = "Hello World";
Scanner scanner = new Scanner(input);
```

#### Basic Methods

The **Scanner** class provides numerous methods to read different types of input:

**Reading a String**:

```java
String line = scanner.nextLine(); // Reads a line of text
```

**Reading a Word**:

```java
String word = scanner.next(); // Reads the next token (word)
```

**Reading Different Data Types**:

```java
int i = scanner.nextInt();     // Reads an integer
double d = scanner.nextDouble(); // Reads a double
boolean b = scanner.nextBoolean(); // Reads a boolean
```

#### Checking for Input

Before reading input, it's often useful to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) there is input available:

**Checking for a Next Token**:

```java
if (scanner.hasNext()) {
    String token = scanner.next();
}
```

**Checking for a Specific Data Type**:

```java
if (scanner.hasNextInt()) {
   int i = scanner.nextInt();
}
```

#### Delimiters

The **Scanner** class uses delimiters to separate tokens. By default, it uses whitespace. You can change the delimiter pattern if needed:

```java
scanner.useDelimiter(","); // Sets the delimiter to a comma
```

### Closing the Scanner

It's important to close the **Scanner** object when done to free up [resources](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources):

```java
scanner.close();
```

Here's a complete example that demonstrates reading different types of input from the console:

```java
import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Reading a line of text
        System.out.println("Enter a line of text:");
        String line = scanner.nextLine();
        System.out.println("You entered: " + line);

        // Reading a word
        System.out.println("Enter a word:");
        String word = scanner.next();
        System.out.println("You entered: " + word);

        // Reading an integer
        System.out.println("Enter an integer:");
        if (scanner.hasNextInt()) {
            int i = scanner.nextInt();
            System.out.println("You entered: " + i);
        } else {
            System.out.println("That's not an integer.");
            scanner.next(); // Clear the invalid input
        }

        // Reading a double
        System.out.println("Enter a double:");
        if (scanner.hasNextDouble()) {
            double d = scanner.nextDouble();
            System.out.println("You entered: " + d);
        } else {
            System.out.println("That's not a double.");
            scanner.next(); // Clear the invalid input
        }

        scanner.close();
    }
}
```

### Summary

- **Convenient Parsing**: The **Scanner** class makes it easy to parse primitive types and strings.
- **[Multiple Sources](https://answers.mindstick.com/qa/96531/is-it-possible-to-make-pivot-table-using-multiple-sources-of-data-in-excel)**: It can read from standard input, files, and strings.
- **Flexibility**: Provides methods to check and read various data types.
- **Delimiters**: Uses whitespace [as the default](https://answers.mindstick.com/qa/95655/how-do-you-set-firefox-as-the-default-browser) delimiter but allows custom delimiters.
- **[Resource Management](https://www.mindstick.com/articles/341532/how-ai-driven-resource-management-in-servicenow-is-transforming-workforce-planning)**: Remember to close the **Scanner** object to release resources.

The **Scanner** class is a versatile and powerful tool for handling input in Java [applications](https://www.mindstick.com/articles/12847/how-to-choose-the-right-ethernet-cable-for-industrial-applications), simplifying the process of reading and parsing [user input](https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input) or data from other sources.

---

Original Source: https://www.mindstick.com/blog/304297/overview-of-java-scanner-class

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
