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
Note:
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 :

NoSuchElementException

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

IllegalStateException

– 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.

package com.javatutorialhq.java.tutorial.scanner;

import java.util.Scanner;

/*
 * This is a java example source code that shows how to use
 * skip method of Scanner class.
 * Split the string into integer tokens skipping
 * string that satisfies the pattern specified
 */

public class ScannerSkipDemo {

	public static void main(String[] args) {

		// Initialize Scanner object
		Scanner scan = new Scanner("test 12 13 15 123");
		// initialize the String pattern
		String pattern = "[a-zA-Z]*";
		// Printing the tokenized Strings
		while(scan.hasNext()){
			// skipping first occurrence of the Pattern
			scan.skip(pattern);
			System.out.println(scan.next());
		}
		// closing the scanner stream
		scan.close();
		//scan.next(pattern);
	}

}

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 :

Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.skip(Unknown Source)
	at java.util.Scanner.skip(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerSkipDemo.main(ScannerSkipDemo.java:24)

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.skip(Unknown Source)
	at java.util.Scanner.skip(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerSkipDemo.main(ScannerSkipDemo.java:30)

Similar Method :

  • N/A

Suggested Reading List :

References :