skip(Pattern): This method is available in java.util.Scanner class of Java.
Syntax:
Scanner java.util.Scanner.skip(Pattern pattern)
This method takes one argument. This method skips input that matches the specified pattern, ignoring delimiters.
Parameters: One parameter is required for this method.
pattern: a string specifying the pattern to skip over.
Returns: this scanner.
Throws:
1. NoSuchElementException - if the specified pattern is not found.
2. IllegalStateException - if this scanner is closed
Approach 1: When no exception
Java
import java.util.Scanner;import java.util.regex.Pattern;public class Scannerskip {public static void main(String[] args) {String source = "Hello World";Scanner scanner = new Scanner(source);Pattern pattern = Pattern.compile("...lo");System.out.println(scanner.skip(pattern));scanner.close();}}
Output:
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=5][match valid=true][need input=false][source closed=false][skipped=false][group separator=\x{2c}][decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]
Approach 2: NoSuchElementException
Java
import java.util.Scanner;import java.util.regex.Pattern;public class Scannerskip {public static void main(String[] args) {String source = "Hello World";Scanner scanner = new Scanner(source);Pattern pattern = Pattern.compile("...loo");System.out.println(scanner.skip(pattern));scanner.close();}}
Output:
Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.skip(Scanner.java:1843)
Approach 3: IllegalStateException
Java
import java.util.Scanner;import java.util.regex.Pattern;public class Scannerskip {public static void main(String[] args) {String source = "Hello World";Scanner scanner = new Scanner(source);Pattern pattern = Pattern.compile("...lo");scanner.close();System.out.println(scanner.skip(pattern));}}
Output:
Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.base/java.util.Scanner.ensureOpen(Scanner.java:1150) at java.base/java.util.Scanner.skip(Scanner.java:1827)
No comments:
Post a Comment