Java Scanner Methods

Beginner
6 min read
Updated: Nov 2026
36 method guides
Method index

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.

Same methods for keyboard, strings, and files.

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:

Java
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 methods

Read 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).

MethodDescriptionTutorial
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 methods

Test what comes next without consuming any input. Use these methods to validate user input, stop loops cleanly, and avoid InputMismatchException or NoSuchElementException.

MethodDescriptionTutorial
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 methods

Inspect 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.

MethodDescriptionTutorial
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 methods

Search text ignoring delimiters, inspect capture groups from the last match, stream remaining tokens (Java 9+), check hidden read errors, and close the scanner safely.

MethodDescriptionTutorial
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

They are the instance methods of java.util.Scanner for breaking text into tokens and reading strings, whole numbers, decimals, booleans, lines, and regex matches from System.in, a String, or a file.
This index lists 36 guides, from close() and delimiter() to useLocale() and useRadix(), grouped into four categories.
Scanner reads text from System.in, a String, or a file with identical tokenizing logic. Passing a String lets you run every example immediately with deterministic output, and keyboard-focused pages also include a System.in sample run.
Only tokens() requires Java 9 or newer. Every other method here, including reset() from Java 6, compiles and runs on Java 8 and later.
Read the Java Scanner hub first. Then open next(), nextLine(), nextInt(), hasNextInt(), and useDelimiter() from the tables below.

Start with next()

Read your first token from input and see how Scanner splits text at delimiters.

next() 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.

12 people found this page helpful