Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

The Scanner class

Reference for java.util.Scanner: reading tokens and lines from System.in, strings and files, the nextInt/nextLine trap, delimiters and hasNext checks.

java.util.Scanner breaks text into tokens using a delimiter pattern (whitespace by default) and converts tokens to numbers, booleans and strings. The source can be System.in, a String, a File, a Path, a Readable or an InputStream. It is the most common way to read console input in Java programs and a convenient parser for small, well-formed text.

A scanner reads ahead into an internal buffer, so you should not mix it with other readers on the same stream. It implements AutoCloseable; closing it closes the underlying source, which for System.in is usually not what you want in a larger program.

Java
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String rest = in.nextLine();

Try it

Java Reading tokens and lines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Strings, delimiters and exceptions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Scanner implements Iterator<String>, Closeable
public Scanner(InputStream source)
public Scanner(String source)
public Scanner(Path source) throws IOException
Method Description
next() Next token as a String.
nextLine() Rest of the current line, without the line break. May return "".
nextInt(), nextLong(), nextDouble(), nextBoolean(), nextBigDecimal() Parse the next token. Overloads such as nextInt(int radix) choose the base.
hasNext(), hasNextLine(), hasNextInt(), hasNextDouble() Look ahead without consuming.
useDelimiter(String or Pattern) Sets the token separator. Returns the scanner.
useLocale(Locale) Sets the locale used for number parsing.
findInLine(String), findAll(String), skip(String) Pattern-based reading that ignores delimiters.
tokens() A Stream<String> of the remaining tokens (Java 9).
close() Closes the source.

Throws: NoSuchElementException when no input is left (nextLine also), InputMismatchException (a subclass) when the token does not match the requested type, and IllegalStateException if the scanner is closed. Numeric nextXxx methods leave a mismatching token unconsumed.

Usage notes

  • The nextInt plus nextLine trap. nextInt() stops before the line break, so the next nextLine() returns the empty remainder of that line. Call nextLine() once to discard it, or read every line with nextLine() and parse it yourself.
  • Guard with hasNextXxx. Reading until end of input is while (in.hasNextLine()). Calling nextInt() blindly on user input will eventually throw.
  • Locale matters. nextDouble() follows the scanner's locale; in some locales 4.5 is rejected and 4,5 accepted. Call useLocale(Locale.ROOT) or Locale.US for portable parsing.
  • Speed. Scanner uses regular expressions per token and is slow on millions of tokens. For large inputs use BufferedReader with split or StringTokenizer.
  • System.in and close(). Closing a scanner on System.in closes standard input for the whole program. Create one scanner and reuse it.
  • Not thread-safe. Do not share an instance between threads.
  • Errors are hidden for I/O. Scanner swallows IOException from its source; check ioException() if it matters.
  • Since Java 25 you can also use IO.readln() for simple prompts; see the user input lesson.