Skip to content
Tutorials Center
My learning

Topics

Lesson 122 of 201 ยท Input and Output

Scanner Patterns and Pitfalls

Master Java's Scanner - token versus line reading, custom delimiters, the newline trap, hasNext loops, regex methods, locales and when to choose BufferedReader instead.

You met Scanner in Reading User Input: you create one, call nextInt() or nextLine(), and your program reacts to what the user typed. That first lesson is enough to write small programs. Real programs, though, hit the same handful of surprises again and again: a blank line where a name should be, a number that won't parse because of a comma, an input loop that never ends, or a program that's painfully slow on a large file.

This lesson goes under the hood. You'll learn how a Scanner really walks through its input, how to change what counts as a separator, why the famous newline trap happens (and the three ways to avoid it), how to use hasNext checks and regular expressions, and when another tool is the better choice. Every example scans a String or the Input box, so you can run them as they are.

How a Scanner reads: a cursor in a buffer

A Scanner doesn't "receive" values. It holds a buffer of text and a cursor (a position) inside it. Every next... method looks at the text from the cursor on, finds something that matches, returns it and moves the cursor past it. Nothing is ever read backwards.

flowchart LR
    A["Text: 42 Ada Lovelace newline"] --> B["Cursor before 42"]
    B -->|"nextInt()"| C["Cursor after 42<br/>(before the space)"]
    C -->|"next()"| D["Skips the space, returns Ada,<br/>cursor after Ada"]
    D -->|"nextLine()"| E["Returns ' Lovelace',<br/>cursor after the newline"]

Each call returns text and moves the cursor; the separators between tokens are skipped only by the token methods.

Keep this picture in mind. Almost every Scanner bug is a question of where the cursor is standing.

Tokens versus lines

Scanner has two families of methods:

  • Token methods (next(), nextInt(), nextDouble(), nextBoolean(), ...) first skip the delimiter (by default any run of whitespace, including line breaks), then return the characters up to the next delimiter. They never return empty tokens.
  • Line methods (nextLine(), hasNextLine()) don't skip anything. nextLine() returns everything from the cursor to the next line break, then moves the cursor past the break. It can return an empty string.
Java Tokens ignore layout, lines keep it

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.

Notice what happened. The token scanner found four words and didn't care about the extra spaces or the blank line. The line scanner reports a blank line 2 and keeps the leading spaces of line 3. If you need to preserve the layout of the input (a poem, a pasted paragraph, a CSV row), use lines; if you only care about the values, use tokens.

One more detail: a text ending in \n does not produce a final empty line. After the last nextLine() the cursor is at the very end, so hasNextLine() is false.

The newline trap, explained and fixed

This is the bug from the first input lesson, now with the cursor picture. Given the input 42\nAda Lovelace\n:

  1. nextInt() skips nothing, reads 42 and stops right before the \n.
  2. nextLine() reads from the cursor to the next line break: that's zero characters. It returns "" and consumes the \n.
  3. The name is still waiting in the buffer, unread.
Java The trap and three fixes

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.

Which fix should you pick?

  • Fix 1 (a throwaway nextLine()) is the quickest and the one you'll see most. It discards whatever else is on the number's line, so use it only when that rest of the line is meant to be empty.
  • Fix 2 (always nextLine(), then Integer.parseInt) is the most predictable. The cursor is always at the start of a line, so you never have to think about it. It's also easy to give a clear error when a line isn't a number.
  • Fix 3 (skip("\\R")) says exactly what you mean: skip one line break. \R is the regex for any line break, \n or \r\n.

Tip: Pick one style per program and stick to it. Mixing token methods and nextLine() is where the trap lives. If every read in your program is a nextLine(), the trap can't happen.

Changing the delimiter

By default tokens are separated by whitespace. useDelimiter(...) takes a regular expression (see Regular Expressions) that describes the separator instead. It's the quickest way to split comma-separated values without String.split:

Java Custom delimiters

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.

Two points to remember:

  • With a single-character delimiter such as ,, the whitespace around the values becomes part of the tokens (case 1). Use a pattern like \s*,\s* to remove it.
  • Once you replace the default delimiter, line breaks are no longer separators unless your pattern includes them (case 3 uses ,|\R). This surprises people who set useDelimiter(",") and then find that the last value of one line and the first value of the next run together.

Scanner is not a full CSV parser: it knows nothing about quoted fields such as "Smith, John". For anything beyond simple separators, use a real CSV library.

Look before you read: the hasNext family

Every next... method has a hasNext... partner that checks without moving the cursor. Used in a loop condition, they turn an exception-prone read into a safe one:

Check True when the next input is
hasNext() any token (so the input isn't exhausted)
hasNextInt(), hasNextLong(), hasNextDouble(), hasNextBoolean() a token that can be read as that type
hasNext("regex") a token that matches the pattern
hasNextLine() any more text, even an empty line

A very useful fact: when nextInt() fails with an InputMismatchException, the bad token is not consumed. If you catch the exception and loop, you'll read the same bad token again, forever. Either check with hasNextInt() first, or discard the token with next() in the catch block.

Java Summing numbers, skipping junk

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.

Look at what was skipped: 3.5 is not an int, and 99999999999 is a valid number but too big for an int, so hasNextInt() is false for both. If you want the large value, test hasNextLong() first.

Every numeric reader

Besides nextInt(), nextLong(), nextDouble() and nextBoolean(), a Scanner has nextByte(), nextShort(), nextFloat(), nextBigInteger() and nextBigDecimal(), each with a matching hasNextXxx(). A value that is a valid number but does not fit the requested type (such as 40000 for a short) throws InputMismatchException and the token stays unread, so skip it with next() if you want to continue.

Java nextByte, nextShort, nextBigInteger

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.

Searching with regular expressions

Scanner can also look inside text instead of splitting it:

  • findInLine(regex) searches the rest of the current line for a match, returns it (or null) and moves the cursor past it.
  • skip(regex) skips input that matches at the cursor, and throws NoSuchElementException if it doesn't match.
  • findAll(regex) returns a Stream<MatchResult> over the whole input, and tokens() returns the remaining tokens as a Stream<String>.
Java Extracting data with findAll, skip and tokens

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.

For heavy text processing, Pattern and Matcher are faster and more flexible; Scanner's regex methods shine in short parsing jobs like these.

Reading to the end of the input

How you detect "nothing left" depends on the method you use:

  • hasNext() / hasNextInt() are the right loop conditions for token reading.
  • hasNextLine() is the right one for line reading.
  • Calling next(), nextInt() or nextLine() when there is nothing left throws NoSuchElementException. With nextInt() on a token that is not a number you get InputMismatchException instead (a subclass of NoSuchElementException).

This program reads lines from the Input box until the input ends, counting words and blank lines:

Java Read until end of input

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.

Sources, locales and closing

A Scanner can read from many sources: new Scanner(System.in) for the console, new Scanner(String) for text you already have (very handy for tests), new Scanner(Path) or new Scanner(File) for files (these throw IOException, so use try-with-resources as in Reading and Writing Files), and new Scanner(InputStream) for any stream. For files, remember the character set from Charsets and Encoding: pass it explicitly, for example new Scanner(path, StandardCharsets.UTF_8).

Two more pitfalls:

  • Locale. nextDouble() follows the default locale, so on a German machine 3,5 is a number and 3.5 is not. For programs that must behave the same everywhere, call scanner.useLocale(Locale.ROOT).
  • Closing. Closing a Scanner also closes the stream under it. If that stream is System.in, you can never read from the console again in this program. Create one Scanner for System.in, share it, and don't close it until you are done.

When not to use Scanner

Scanner is convenient, not fast. It runs a regular expression for every token, and it silently swallows IOExceptions (call ioException() to see the last one). For a program that reads hundreds of thousands of numbers, such as a coding competition or a data import, a BufferedReader plus String.split or StringTokenizer is several times faster. For structured data use a proper parser, for example the CSV, JSON or properties tools you'll meet in Properties and Configuration.

A simple rule: use Scanner for interactive and small input, BufferedReader for big input, and a parser library for formats.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A program calls nextInt() and then nextLine() on the input 7 followed by a line break and Maya. What does nextLine() return?

    nextInt() stops right before the line break, so nextLine() reads the empty rest of the first line. The name is still unread.
  2. 2. You call useDelimiter(",") on a Scanner over a, b ,c. What is the second token?

    A plain comma delimiter leaves the whitespace inside the tokens. Use the pattern \s*,\s* to remove it.
  3. 3. nextInt() throws an InputMismatchException on the token abc. What happens to that token?

    A failed read does not consume the token. Check hasNextInt() first, or call next() to discard the token, otherwise a retry loop never ends.
  4. 4. Which situation makes BufferedReader a better choice than Scanner?

    Scanner runs regular expressions for every token, which makes it slow on large inputs. BufferedReader is much faster.

Summary

  • A Scanner is a cursor in a buffer; every call returns text and moves the cursor forward.
  • Token methods skip delimiters and never return empty tokens; nextLine() returns the rest of the current line, possibly empty.
  • The newline trap comes from nextInt() leaving the line break behind. Avoid it by reading only lines, adding one throwaway nextLine(), or using skip("\\R").
  • useDelimiter takes a regular expression; a single-character delimiter keeps surrounding spaces, and a custom delimiter stops line breaks from separating tokens unless you include them.
  • Use hasNextInt() and friends before reading; a failed nextInt() does not consume the bad token.
  • findInLine, skip, findAll and tokens() bring regular expressions and streams to Scanner.
  • Create one Scanner for System.in, don't close it early, and set Locale.ROOT when decimal separators matter.
  • For big inputs choose BufferedReader; for structured formats choose a parser.

Next: NIO.2: Path, Files and WatchService, the modern file API, including watching a folder for changes.