Java Scanner skip() Method

The skip() is a method of Java Scanner class which skips input that matches the specified pattern, ignoring delimiters. There are two different types of Java Scanner skip() method which can be differentiated depending on its parameter. These are:

  1. Java Scanner skip(String pattern) Method
  2. Java Scanner skip(Pattern pattern) Method

1. Java Scanner skip(String pattern) Method

It is a Scanner class method which skips input that matches the specified pattern, ignoring delimiters.

2. Java Scanner skip(Pattern pattern) Method

It is a Scanner class method which skips input that matches a pattern constructed from the specified string.

Syntax

Following are the declarations of skip() method:

Parameter

DataTypeParameterDescriptionRequired/Optional
StringpatternIt is a string specifying the pattern to skip.Required
PatternpatternIt is the pattern to skip for specified string.Required

Returns

The skip() method returns this Scanner object.

Exceptions

NoSuchElementException- It will thrown this Exception if the specified pattern not found.

IllegalStateException- It will thrown this Exception if the innvocation is done after Scanner is closed.

Compatibility Version

Java 1.5 and above

Example 1

Output:

Left String:  World! SSSIT 102 Hindi100 150 123

Example 2

Output:

102
131
150
123

Example 3

Output:

SSSIT 102 Hindi100 150 123

Example 4

Output:

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.skip(Scanner.java:1843)
	at java.base/java.util.Scanner.skip(Scanner.java:1860)
	at myPackage.ScannerSkipExample4.main(ScannerSkipExample4.java:11)