The Java Scanner skip(Pattern pattern) method skips input that matches the specified pattern, ignoring delimiters. This method will skip input if an anchored match of the specified pattern succeeds.If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown.
DeclarationFollowing is the declaration for java.util.Scanner.skip() method
public Scanner skip(Pattern pattern)Parameters
pattern − a string specifying the pattern to skip over
Return ValueThis method returns this scanner
ExceptionNoSuchElementException − if the specified pattern is not found
IllegalStateException − if this scanner is closed
The Java Scanner skip(String pattern) method skips input that matches a pattern constructed from the specified string.An invocation of this method of the form skip(pattern) behaves in exactly the same way as the invocation skip(Pattern.compile(pattern)).
DeclarationFollowing is the declaration for java.util.Scanner.skip(String pattern) method
public Scanner skip(String pattern)Parameters
pattern − a string specifying the pattern to skip over
Return ValueThis method returns this scanner
ExceptionIllegalStateException − if this scanner is closed
Skipping a Token based on Pattern of a Scanner on a String ExampleThe following example shows the usage of Java Scanner skip(Pattern pattern) method to skip a pattern in a given string. We've created a scanner object using a given string. We've retrieved a stream using findAll(Pattern) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.
package com.tutorialspoint; import java.util.Scanner; import java.util.regex.Pattern; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6.0 true "; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // skip the word that matches the pattern ..llo scanner.skip(Pattern.compile("..llo")); // print a line of the scanner System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }Output
Let us compile and run the above program, this will produce the following result −
World! 3 + 3.0 = 6.0 trueSkipping a Token based on String of a Scanner on a String Example
The following example shows the usage of Java Scanner skip(String pattern) method to skip a pattern in a given string. We've created a scanner object using a given string. We've retrieved a stream using findAll(String) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6.0 true "; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // skip the word that matches the pattern ..llo scanner.skip("..llo"); // print a line of the scanner System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }Output
Let us compile and run the above program, this will produce the following result −
World! 3 + 3.0 = 6.0 trueSkipping a Token based on Pattern of a Scanner on User Input Example
The following example shows the usage of Java Scanner skip(String pattern) method to skip a pattern in a given string. We've created a scanner object using System.in class. We've retrieved a stream using findAll(String) method. A stream is then iterated to print the result. Then we printed the string using scanner.nextLine() method.
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { // create a new scanner with the System input Scanner scanner = new Scanner(System.in); // skip the word that matches the pattern ..llo scanner.skip("..llo"); // print a line of the scanner System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }Output
Let us compile and run the above program, this will produce the following result − (We've entered Hello World.)
Hello World World
java_util_scanner.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4