Scanner skip() method in Java with Examples

skip(Pattern pattern)

The skip(Pattern pattern) method of java.util.Scanner class skips input that matches the specified pattern, ignoring the delimiters. The function skips the input if an anchored match of the specified pattern succeeds it.

Syntax:

public Scanner skip(Pattern pattern)

Parameters: The function accepts a mandatory parameter pattern which specifies a string as pattern to be skipped.

Return Value: The function returns this scanner

Exceptions: This method throws following exceptions:

  • NoSuchElementException: when the specified pattern is not found
  • IllegalStateException: when this scanner is closed

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// skip() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "GeeksForGeeks - "
                   + "A Computer Science Portal for Geeks";
  
        System.out.println("String trying to get input:\n"
                           + s);
  
        // create a new scanner with
        // the specified String Object
        Scanner scanner = new Scanner(s);
  
        // skip the word that
        // matches with the pattern ..eks
        System.out.println("Skipping 5 letter words"
                           + " that ends with 'eks'\n");
  
        scanner.skip(Pattern.compile("..eks"));
  
        // print a line of the scanner
        System.out.println("Input Scanner String: \n"
                           + scanner.nextLine());
  
        // close the scanner
        scanner.close();
    }
}

chevron_right


Output:

String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 5 letter words that ends with 'eks'

Input Scanner String: 
ForGeeks - A Computer Science Portal for Geeks

Program 2: To demonstrate NoSuchElementException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// skip() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks";
  
            System.out.println("String trying to get input:\n"
                               + s);
  
            // create a new scanner with
            // the specified String Object
            Scanner scanner = new Scanner(s);
  
            // skip the word that
            // matches with the pattern and
            System.out.println("Skipping 3 letter words"
                               + " and\n");
  
            scanner.skip(Pattern.compile("and"));
  
            // print a line of the scanner
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine());
  
            // close the scanner
            scanner.close();
        }
  
        catch (Exception e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 3 letter words and

Exception thrown: java.util.NoSuchElementException

Program 3: To demonstrate IllegalStateException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// skip() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks";
  
            System.out.println("String trying to get input:\n"
                               + s);
  
            // create a new scanner with
            // the specified String Object
            Scanner scanner = new Scanner(s);
  
            // close the scanner
            scanner.close();
            System.out.println("Scanner Closed");
  
            // skip the word that
            // matches with the pattern and
            System.out.println("Trying to Skip 3 letter words"
                               + " and\n");
  
            scanner.skip(Pattern.compile("and"));
  
            // print a line of the scanner
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine());
        }
  
        catch (Exception e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Scanner Closed
Trying to Skip 3 letter words and

Exception thrown: java.lang.IllegalStateException: Scanner closed

skip(String pattern)

The skip(String pattern) method of java.util.Scanner class skips the input that matches with the pattern constructed from the specified string. The skip(pattern) and skip(Pattern.compile(pattern)) behaves exactly the same way on being called.

Syntax:

public Scanner skip(String pattern)

Parameters: The function accepts a mandatory parameter string pattern which specifies a string denoting the pattern to skip over

Return Value: The function returns this scanner

Exceptions: This method throws IllegalStateException when this scanner is closed

Below programs illustrate the above function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// skip() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        String s = "GeeksForGeeks - "
                   + "A Computer Science Portal for Geeks";
  
        System.out.println("String trying to get input:\n"
                           + s);
  
        // create a new scanner with
        // the specified String Object
        Scanner scanner = new Scanner(s);
  
        // skip the word that
        // matches with the pattern ..eks
        System.out.println("Skipping 5 letter words"
                           + " that ends with 'eks'\n");
  
        scanner.skip("..eks");
  
        // print a line of the scanner
        System.out.println("Input Scanner String: \n"
                           + scanner.nextLine());
  
        // close the scanner
        scanner.close();
    }
}

chevron_right


Output:

String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Skipping 5 letter words that ends with 'eks'

Input Scanner String: 
ForGeeks - A Computer Science Portal for Geeks

Program 2: To demonstrate IllegalStateException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// skip() method of Scanner class in Java
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
            String s = "GeeksForGeeks - "
                       + "A Computer Science Portal for Geeks";
  
            System.out.println("String trying to get input:\n"
                               + s);
  
            // create a new scanner with
            // the specified String Object
            Scanner scanner = new Scanner(s);
  
            // close the scanner
            scanner.close();
            System.out.println("Scanner Closed");
  
            // skip the word that
            // matches with the pattern and
            System.out.println("Trying to Skip 3 letter words"
                               + " and\n");
  
            scanner.skip("and");
  
            // print a line of the scanner
            System.out.println("Input Scanner String: \n"
                               + scanner.nextLine());
        }
  
        catch (Exception e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}

chevron_right


Output:

String trying to get input:
GeeksForGeeks - A Computer Science Portal for Geeks
Scanner Closed
Trying to Skip 3 letter words and

Exception thrown: java.lang.IllegalStateException: Scanner closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#skip(java.util.regex.Pattern)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.