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:
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);
Scanner scanner = new Scanner(s);
System.out.println( "Skipping 5 letter words"
+ " that ends with 'eks'\n" );
scanner.skip(Pattern.compile( "..eks" ));
System.out.println( "Input Scanner String: \n"
+ scanner.nextLine());
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
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);
Scanner scanner = new Scanner(s);
System.out.println( "Skipping 3 letter words"
+ " and\n" );
scanner.skip(Pattern.compile( "and" ));
System.out.println( "Input Scanner String: \n"
+ scanner.nextLine());
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
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);
Scanner scanner = new Scanner(s);
scanner.close();
System.out.println( "Scanner Closed" );
System.out.println( "Trying to Skip 3 letter words"
+ " and\n" );
scanner.skip(Pattern.compile( "and" ));
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:
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);
Scanner scanner = new Scanner(s);
System.out.println( "Skipping 5 letter words"
+ " that ends with 'eks'\n" );
scanner.skip( "..eks" );
System.out.println( "Input Scanner String: \n"
+ scanner.nextLine());
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
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);
Scanner scanner = new Scanner(s);
scanner.close();
System.out.println( "Scanner Closed" );
System.out.println( "Trying to Skip 3 letter words"
+ " and\n" );
scanner.skip( "and" );
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)
Last Updated :
11 Oct, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...