Java Utility Library

Java Scanner - skip() Method



The java.util.Scanner.skip() method is used to skip input that matches a pattern constructed from the specified string.

Syntax

public Scanner skip(String pattern)

Parameters

pattern Specify a string specifying the pattern to skip over.

Return Value

Returns the scanner.

Exception

Throws IllegalStateException, if this scanner is closed.

Example:

In the example below, the java.util.Scanner.skip() method is used to skip input that matches the specified pattern, ignoring delimiters.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {

    //String to scan
    String MyString = "Hello World 10 + 20 = 30.0";

    //creating a Scanner
    Scanner MyScan = new Scanner(MyString);

    //skip the first word if matches the word Hello
    MyScan.skip("Hello");
      
    //print the rest of the line
    System.out.println(MyScan.nextLine());

    //close the scanner
    MyScan.close();
  }
}

The output of the above code will be:

 World 10 + 20 = 30.0

❮ Java.util - Scanner