java.util.Scanner skip()

Description :

This java tutorial shows how to use the skip method of Scanner class of java.util package. This method returns this Scanner object which have its elements skipped that matched the pattern method argument.

Method Syntax :

There are two overloaded method of Scanner skip java method.

java scanner skip overloaded method
Overloaded methods are those method with same name but with different method signatures

Method 1 : 

public Scanner skip(Pattern pattern)

Method 2:

public Scanner skip(String pattern)

Parameter Input :


DataType Parameter Description
String Pattern a string specifying the pattern to skip
Pattern Pattern The pattern to skip for by this Scanner


Method Returns :

The skip method returns this Scanner object with it’s token skipped that satisfies the pattern specified in the method argument. If the pattern was found, the Scanner object skipped the string that satisfies the pattern specified and returns the string that matched the pattern. If the pattern was not found, the scanner will return null and the position will not changed.

Compatibility Version :

Requires Java 1.5 and up

Exception :


– The NoSuchElementException will be thrown if and only if the specified pattern was not found.


– The IllegalStateException will be thrown by this Scanner object if we call the skip method after the scanner has been closed.

Discussion :

Basically the two overloaded findWithinHorizon method do the same tasks. Mean to say that invoking skip(String pattern) is the same as skip(Pattern.compile(String pattern),int horizon).

Java Code Example :

This java example source code demonstrates the use of skip method of Scanner class.

Sample Output :

Running the skip method example source code of Scanner class will give you the following output

java scanner skip(pattern) method example

java scanner skip(pattern) method example

Exception Scenario :

Similar Method :

  • N/A

Suggested Reading List :

References :