Java Scanner reset() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

In Java, sc.reset() (added in Java 6) resets the scanner's three configuration settings—its delimiter (\p{javaWhitespace}+), locale (default format locale), and radix (10)—and returns the Scanner itself.

Crucially, reset() does not rewind or clear the input stream: reading continues from the exact cursor position where you left off.

An instance method of java.util.Scanner, added in Java 6 (JDK 1.6). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.reset(...).

Syntax

Call sc.reset() when you want to undo earlier useDelimiter(), useLocale(), or useRadix() changes at once.

Java
sc.useDelimiter(",").useLocale(Locale.GERMANY).useRadix(16);
// ... parse custom section ...
sc.reset();   // back to whitespace delimiter, default locale, radix 10
CallWhat it does
sc.reset()Restores default delimiter, locale, and radix without moving the cursor.
sc.reset().nextInt()Chaining works because reset() returns this.

Added in Java 6 (JDK 1.6), whereas Scanner itself dates from Java 5. Calling reset() on a closed scanner does not throw; only the read methods do.

Library declarations
Java
// java.util.Scanner (Java 6+)
public Scanner reset()

Parameters

ParameterDescription
Nonereset() takes no parameters.

Return Value

Return ValueDescription
ScannerThe same scanner instance with default delimiter, locale, and radix restored.
ThrowsNothing. It does not check whether the scanner is closed.

Compare with Similar Methods

Resetting all settings vs individual setters:

MethodHow it differs
useDelimiter(p)Restores or changes just the delimiter pattern.
useLocale(loc)Restores or changes just the number-parsing locale.
useRadix(r)Restores or changes just the integer base.

Java Examples

Example 1 — Reset Radix and Delimiter While Continuing Forward in Input

Read a binary token with a custom separator, call reset(), and see that reading continues forward in base 10.

Java
import java.util.Scanner;

public class ScannerResetForward {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("1010 25").useDelimiter(" ").useRadix(2)) {
            int first = sc.nextInt();
            System.out.println("Before reset (radix " + sc.radix() + "): " + first);
            sc.reset();
            System.out.println("After reset: radix=" + sc.radix() + ", delim=" + sc.delimiter());
            int second = sc.nextInt();
            System.out.println("Next value (not rewound): " + second);
        }
    }
}

How It Works

  1. Custom settings active first. "1010" reads in base 2 as 10, leaving the cursor right before " 25".
  2. Settings snap back to defaults. Calling sc.reset() restores radix 10 and delimiter \p{javaWhitespace}+.
  3. Cursor never rewinds. The second nextInt() skips the space and reads 25 (in decimal) rather than re-reading "1010".

Example 2 — Parse a Custom Header Section, Then Reset for Plain Words

Change delimiter, radix, and locale for line 1, clear the newline, then call reset() for line 2.

Java
import java.util.Locale;
import java.util.Scanner;

public class ScannerResetSection {
    public static void main(String[] args) {
        String doc = "ff;3,5\nplain 99";
        try (Scanner sc = new Scanner(doc)) {
            sc.useDelimiter("[;\\n]").useLocale(Locale.GERMANY).useRadix(16);
            int id = sc.nextInt();
            double weight = sc.nextDouble();
            sc.nextLine();

            sc.reset();
            String label = sc.next();
            int count = sc.nextInt();
            System.out.println("Header -> id=" + id + ", weight=" + weight);
            System.out.println("Body   -> label=" + label + ", count=" + count);
        }
    }
}

How It Works

  1. Three overrides at once. Line 1 reads hex ff (255) and German decimal 3,5 separated by ; or \n.
  2. Consume line end, then reset. Advancing past \n with nextLine() puts the cursor on "plain 99" before reset() runs.
  3. Default parsing returns cleanly. next() and nextInt() read "plain" and 99 with whitespace and base 10 restored.

Example 3 — See reset() Stay Quiet After close(), While Reading Throws

Verify chaining on an open scanner works, and that reset() on a closed scanner does not throw, but the next read does.

Java
import java.util.Scanner;

public class ScannerResetClosed {
    public static void main(String[] args) {
        Scanner sc = new Scanner("12").useRadix(16);
        int decimalVal = sc.reset().nextInt();
        System.out.println("Chained reset().nextInt() -> " + decimalVal);
        sc.close();
        sc.reset();
        System.out.println("reset() after close: no exception");
        try {
            sc.nextInt();
        } catch (IllegalStateException e) {
            System.out.println("nextInt() after close: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Radix resets before reading. Calling reset() reverts radix from 16 back to 10, so "12" parses as decimal 12 rather than hex 18.
  2. reset() ignores the closed state. After sc.close(), calling sc.reset() still works and prints no error, because it only resets settings.
  3. Reading a closed scanner fails. The later sc.nextInt() throws IllegalStateException; chaining sc.reset().nextInt() on an open scanner works fine.

Frequently Asked Questions

No. reset() only restores delimiter, locale, and radix to their defaults; your current position in the input stays where it is.
Java 6 (JDK 1.6), one release after Scanner itself arrived in Java 5.
The delimiter (back to \p{javaWhitespace}+), the locale (back to the JVM default format locale), and the radix (back to 10).
No—it restores the JVM default format locale, which depends on the system. Call useLocale(Locale.US) again if you require dot decimals.
Nothing. reset() only changes settings, so it does not throw. The next read call, such as nextInt(), throws IllegalStateException.

Next: skip()

Skip input matching a pattern right at the current position.

skip() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful