skip(String): This method is available in java.util.Scanner class of Java.
Syntax:
Scanner java.util.Scanner.skip(String pattern)
This method takes one argument. This method skips input that matches a pattern constructed from the specified string.
Parameters: One parameter is required for this method.
pattern: a string specifying the pattern to skip over.
Returns: this scanner.
Throws:
IllegalStateException - if this scanner is closed
Approach 1: When no exception
Java
import java.util.Scanner;public class Scannerskip2 {public static void main(String[] args) {String source = "Hello World";Scanner scanner = new Scanner(source);String pattern = "...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: IllegalStateException
Java
import java.util.Scanner;public class Scannerskip2 {public static void main(String[] args) {String source = "Hello World";Scanner scanner = new Scanner(source);String pattern = "...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) at java.base/java.util.Scanner.skip(Scanner.java:1860)
No comments:
Post a Comment