Java Scanner Methods

About This Index
This page is the Java Scanner Methods index: every public instance method of java.util.Scanner, each with a complete guide covering syntax, all overloads, parameters, return values, exceptions, three runnable examples, and FAQs. Most examples pass fixed text into new Scanner("...") so outputs are completely deterministic; keyboard-focused pages also show a System.in sample run.
Learn what a Scanner is and how the nextInt() / nextLine() newline pitfall happens on the Scanner hub. Use this page when you need a specific method such as next(), nextLine(), nextInt(), or useDelimiter(). All examples compile on Java 8 and later, except tokens(), which requires Java 9 or newer.
Scanner wraps System.in, a String, or a file/Path with identical parsing rules. For decimals, always set useLocale(Locale.US) unless you want the user's regional decimal separator.
How to Call a Scanner Method
Import java.util.Scanner, create a Scanner over your source, check before reading when input might vary, and close string/file scanners when finished:
import java.util.Locale;
import java.util.Scanner;
try (Scanner sc = new Scanner("Anna 25 3.5").useLocale(Locale.US)) {
String name = sc.next(); // "Anna"
int age = sc.nextInt(); // 25
double gpa = sc.nextDouble(); // 3.5
} Java Scanner Methods Table
All method guides in this Java tutorial series (36 tutorials), grouped by purpose.
Read tokens and typed values
11 methodsRead the next whitespace-separated word, the rest of the current line, or a parsed number, boolean, or big number. Every call advances past the value (or throws on mismatch).
| Method | Description | Tutorial |
|---|---|---|
next() | Read and return the next complete token as a String. | Try it |
nextLine() | Read the rest of the current line and skip the line separator. | Try it |
nextInt() | Read the next token as an int. | Try it |
nextLong() | Read the next token as a long. | Try it |
nextShort() | Read the next token as a short. | Try it |
nextByte() | Read the next token as a byte (-128 to 127). | Try it |
nextDouble() | Read the next token as a double. | Try it |
nextFloat() | Read the next token as a float. | Try it |
nextBoolean() | Read the next token as a boolean (true or false). | Try it |
nextBigInteger() | Read the next token as an arbitrary-size BigInteger. | Try it |
nextBigDecimal() | Read the next token as an exact BigDecimal. | Try it |
Check before reading (lookahead)
11 methodsTest what comes next without consuming any input. Use these methods to validate user input, stop loops cleanly, and avoid InputMismatchException or NoSuchElementException.
| Method | Description | Tutorial |
|---|---|---|
hasNext() | Check whether another token is available without consuming it. | Try it |
hasNextLine() | Check whether another line exists in the input (even an empty line). | Try it |
hasNextInt() | Check whether the next token fits in a signed int. | Try it |
hasNextLong() | Check whether the next token fits in a signed long. | Try it |
hasNextShort() | Check whether the next token fits in a signed short. | Try it |
hasNextByte() | Check whether the next token fits in a signed byte (-128 to 127). | Try it |
hasNextDouble() | Check whether the next token can be parsed as a double. | Try it |
hasNextFloat() | Check whether the next token can be parsed as a float. | Try it |
hasNextBoolean() | Check whether the next token is true or false (ignoring case). | Try it |
hasNextBigInteger() | Check whether the next token can be parsed as a BigInteger. | Try it |
hasNextBigDecimal() | Check whether the next token can be parsed as a BigDecimal. | Try it |
Delimiters, locale, radix, and reset
7 methodsInspect or change how the scanner splits tokens, which decimal/grouping symbols it expects, and which number base it uses, or restore all three settings at once.
| Method | Description | Tutorial |
|---|---|---|
delimiter() | Return the Pattern the scanner currently uses to split tokens. | Try it |
useDelimiter() | Change the delimiter Pattern used to separate tokens. | Try it |
locale() | Return the Locale used when parsing numbers. | Try it |
useLocale() | Change the Locale used to parse decimal and grouping separators. | Try it |
radix() | Return the scanner's default number base (10 by default). | Try it |
useRadix() | Change the default number base (2 to 36) for integer reads. | Try it |
reset() | Reset delimiter, locale, and radix to their defaults without rewinding input. | Try it |
Pattern matching, streams, errors, and closing
7 methodsSearch text ignoring delimiters, inspect capture groups from the last match, stream remaining tokens (Java 9+), check hidden read errors, and close the scanner safely.
| Method | Description | Tutorial |
|---|---|---|
findInLine() | Search only the current line for a regex match, ignoring delimiters. | Try it |
findWithinHorizon() | Search across lines up to a character horizon for a regex match. | Try it |
skip() | Skip input matching a pattern right at the current position. | Try it |
match() | Return the MatchResult from the last successful scanning operation. | Try it |
tokens() | Return a lazy Stream<String> of the remaining tokens (Java 9+). | Try it |
ioException() | Return the last IOException thrown by the underlying Readable, or null. | Try it |
close() | Close the scanner and its underlying input source if Closeable. | Try it |
Frequently Asked Questions
Start with next()
Read your first token from input and see how Scanner splits text at delimiters.
12 people found this page helpful
