Why does the following code return false?
Scanner sc = new Scanner(\"-v \");
sc.useDelimiter(\"-[a-zA-Z]\\\\s+\");
System.out.println(sc.hasNext());
Thanks John Kugelman, I think you're right.
Scanner can use customized delimiter to split input into tokens. The default delimiter is a whitespace.
When delimiter doesn't match any input, it'll result all the input as one token:
Scanner sc = new Scanner("-v");
sc.useDelimiter( "-[a-zA-Z]\\s+");
if(sc.hasNext())
System. out.println(sc.next());
In the code above, the delimiter actually doesn't get any match, all the input "-v" will be the single token. hasNext() means has next token.
Scanner sc = new Scanner( "-v ");
sc.useDelimiter( "-[a-zA-Z]\\s+");
if(sc.hasNext())
System. out.println(sc.next());
this will match the delimiter, and the split ended up with 0 token, so the hasNext() is false.