Home Java Scanner Methods reset() Java Scanner reset() Method 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.
sc.useDelimiter(",").useLocale(Locale.GERMANY).useRadix(16);
// ... parse custom section ...
sc.reset(); // back to whitespace delimiter, default locale, radix 10 Call What 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.util.Scanner (Java 6+)
public Scanner reset() Parameters Parameter Description None reset() takes no parameters.
Return Value Return Value Description ScannerThe same scanner instance with default delimiter, locale, and radix restored. Throws Nothing. It does not check whether the scanner is closed.
Important
Beginners sometimes assume reset() rewinds the scanner back to the beginning of a string or file—it does not . To re-read a String from the start, create a new Scanner(text). Note also that reset() sets the locale back to the JVM's default format locale, which varies across machines, so re-apply useLocale(Locale.US) if reading dot decimals afterwards.
Compare with Similar Methods Resetting all settings vs individual setters:
Method How 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 1. Resets settings, not cursor 2. Switch sections cleanly 3. Closed state check 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.
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);
}
}
} Before reset (radix 2): 10
After reset: radix=10, delim=\p{javaWhitespace}+
Next value (not rewound): 25 How It Works Custom settings active first. "1010" reads in base 2 as 10, leaving the cursor right before " 25".Settings snap back to defaults. Calling sc.reset() restores radix 10 and delimiter \p{javaWhitespace}+.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.
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);
}
}
} Header -> id=255, weight=3.5
Body -> label=plain, count=99 How It Works Three overrides at once. Line 1 reads hex ff (255) and German decimal 3,5 separated by ; or \n.Consume line end, then reset. Advancing past \n with nextLine() puts the cursor on "plain 99" before reset() runs.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.
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());
}
}
} Chained reset().nextInt() -> 12
reset() after close: no exception
nextInt() after close: IllegalStateException How It Works Radix resets before reading. Calling reset() reverts radix from 16 back to 10, so "12" parses as decimal 12 rather than hex 18.reset() ignores the closed state. After sc.close(), calling sc.reset() still works and prints no error, because it only resets settings.Reading a closed scanner fails. The later sc.nextInt() throws IllegalStateException; chaining sc.reset().nextInt() on an open scanner works fine.Frequently Asked Questions Does Scanner.reset() rewind back to the beginning of the input? No. reset() only restores delimiter, locale, and radix to their defaults; your current position in the input stays where it is.
Which Java version added Scanner.reset()? Java 6 (JDK 1.6), one release after Scanner itself arrived in Java 5.
What three settings does reset() restore? The delimiter (back to \p{javaWhitespace}+), the locale (back to the JVM default format locale), and the radix (back to 10).
Does reset() restore Locale.US automatically? No—it restores the JVM default format locale, which depends on the system. Call useLocale(Locale.US) again if you require dot decimals.
What happens if I call reset() after closing the scanner? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion