Scanner.skip documentation regarding separators

According to javadoc for java.util.Scanner.skip , this method:

Skips input matching pattern specified, ignoring delimiters .

But I'm confused by what the phrase “ignore delimiters” means, because the following code throws an exception using Java 7 in Eclipse:

 import java.util.Scanner; public class Example { public static void main(String [] args) { Scanner sc = new Scanner("Hello World! Here 55"); String piece = sc.next(); sc.skip("World"); // Line A throws NoSuchElementException, vs. sc.skip("\\sWorld"); // Line B works! sc.findInLine("World"); // Line C works! } } 

It does not ignore delimiters when skipping, as shown in Line A. However, Line C works, although its documentation uses the same phrase “ignore delimiters”. Do I really not understand their concept of "ignoring separators" in this case, or is this a real mistake? What am I missing?

+6
source share
1 answer

You left the following sentence of the description of the method that reads (my selection):

This method skips input if the bound match of the specified pattern is successfully completed.

So, Scanner does not so much “ignore” the delimiter, but simply tries to match the specified regular expression without considering the delimiter. In other words, the space in front of World not considered as a delimiter on skip() , but simply the part of the input with which it is trying to match.

+2
source

Source: https://habr.com/ru/post/1011783/


All Articles