Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaScanner skip() method in Java with Examples

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:




// 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();
    }
}


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




// 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);
        }
    }
}


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




// 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);
        }
    }
}


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:




// 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();
    }
}


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




// 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);
        }
    }
}


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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments