Why does this code throw a InputMismatchException ?
Scanner scanner = new Scanner("hello world");
System.out.println(scanner.next("hello\\s*world"));
The same regex matches in http://regexpal.com/ (with \s instead of \\s)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A Scanner, as opposed to a Matcher, has built in tokenization of the string, the default delimiter is white space. So your “hello world” is getting tokenized into “hello” “world” before the match runs. It would be a match if you changed the delimiter before scanning to something not in the string, eg.:
but it seems like really for your case you should just be using a
Matcher.This is an example of using a Scanner “as intended”:
output would be