Java Scanner skip() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Scanner

Definition

In Java, sc.skip(pattern) skips over text that matches pattern starting at the exact current position, ignoring any delimiter setting, and returns the Scanner itself.

Unlike findInLine(), skip() does not search forward: if the text right at the cursor does not match, it throws NoSuchElementException.

An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). 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.skip(...).

Syntax

Pass a regex String or Pattern that matches the prefix sitting directly at the scanner's cursor.

Java
Scanner sc = new Scanner("ID: 42");
int id = sc.skip("ID:\\s*").nextInt();   // skips "ID: ", reads 42
CallWhat it does
sc.skip("ID:\\s*")Matches right at the cursor, advances past it, and returns this.
sc.skip(pattern)Matches using a compiled Pattern object.
sc.skip("\\R")Skips an expected line break without allocating a line string.

Because skip() returns the Scanner, you can chain sc.skip("prefix:").nextInt() in one readable expression. It also populates match().

Library declarations
Java
// java.util.Scanner
public Scanner skip(Pattern pattern)
public Scanner skip(String pattern)

Parameters

ParameterDescription
patternRegex String or Pattern anchored at the current scanner position.

Return Value

Return ValueDescription
ScannerThe same scanner instance, positioned right after the skipped match.
Throws NoSuchElementExceptionIf the input at the current position does not match pattern.
Throws IllegalStateExceptionIf the scanner has been closed.

Compare with Similar Methods

Anchored skip vs searching and reading:

MethodHow it differs
findInLine(p)Searches ahead on the line instead of matching only at cursor.
next(p)Respects delimiters and returns the matched token string.
match()Inspects what skip() just consumed.

Java Examples

Example 1 — Skip a Label Prefix and Chain nextInt()

Step past "TAG: " in one fluent chain so nextInt() reads the integer immediately after.

Java
import java.util.Scanner;

public class ScannerSkipPrefix {
    public static void main(String[] args) {
        String entry = "TAG: 42 ; TAG: 88";
        try (Scanner sc = new Scanner(entry)) {
            int first = sc.skip("TAG:\\s*").nextInt();
            int second = sc.skip("\\s*;\\s*TAG:\\s*").nextInt();
            System.out.println("First: " + first + ", Second: " + second);
        }
    }
}

How It Works

  1. Immediate prefix match. "TAG:\\s*" matches "TAG: " right at index 0, leaving the cursor at 42 before the space.
  2. Chained read. Returning this lets .nextInt() execute right on the result of skip().
  3. Consume separator cleanly. The second skip("\\s*;\\s*TAG:\\s*") clears " ; TAG: " before reading 88.

Example 2 — Skip Optional Leading Spaces and Punctuation with Pattern

Use a compiled Pattern with \\s* to discard leading spaces and dashes before a number.

Java
import java.util.Scanner;
import java.util.regex.Pattern;

public class ScannerSkipPattern {
    public static void main(String[] args) {
        Pattern sep = Pattern.compile("\\s*--\\s*");
        try (Scanner sc = new Scanner("   -- 77")) {
            sc.skip(sep);
            System.out.println("Skipped text: [" + sc.match().group() + "]");
            System.out.println("Number: " + sc.nextInt());
        }
    }
}

How It Works

  1. Leading spaces included explicitly. Because skip() ignores delimiters, \\s* is required to eat the three spaces before --.
  2. Saved in MatchResult. Calling sc.match().group() reveals the exact substring skipped: " -- ".
  3. Cursor aligned on number. After skipping, nextInt() reads 77 cleanly.

Example 3 — See skip() Throw NoSuchElementException When Not at Start

Contrast skip() (fails if digits are not right at the cursor) with findInLine().

Java
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ScannerSkipMismatch {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner("abc 123")) {
            try {
                sc.skip("\\d+");
            } catch (NoSuchElementException e) {
                System.out.println("skip threw: " + e.getClass().getSimpleName());
            }
            System.out.println("findInLine found: " + sc.findInLine("\\d+"));
        }
    }
}

How It Works

  1. Anchored check rejects prefix "abc". Because digits do not start at position 0, skip("\\d+") throws NoSuchElementException.
  2. Cursor remains at start. Throwing leaves "abc 123" completely unconsumed.
  3. findInLine searches ahead. Switching to findInLine("\\d+") skips "abc " internally and returns "123".

Frequently Asked Questions

It returns the Scanner itself, allowing fluent chains like sc.skip("ID:").nextInt().
Because skip() only matches starting right at the current cursor position ("abc"), not later on the line. Use findInLine() to search ahead.
No. skip() ignores the delimiter pattern, so include \s* in your regex if leading spaces might be present.
Yes. Call sc.match().group() right after skip() succeeds.
It throws IllegalStateException.

Next: tokens()

Return a lazy Streamof the remaining tokens (Java 9+).

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

11 people found this page helpful