get entire line with java.util.scanner.hasNext(regex)

Posted by Hussain on Stack Overflow See other posts from Stack Overflow or by Hussain
Published on 2010-04-24T01:38:20Z Indexed on 2010/04/24 1:43 UTC
Read the original article Hit count: 235

Filed under:
|
|
|
|

I'm doing something in Java that requires input to be matched against the pattern ^[1-5]$. I should have a while loop looping through each line of input, checking it against the pattern, and outputting an error message if it does not.

Sudo code:
while (regex_match(/^[^1-5]$/,inputLine)) {
print ("Please enter a number between 1 and 5! ");
getNextInputLine(); }

I can use java.util.Scanner.hasMatch("^[^1-5]$"), but that will only match a single token, not the entire line. Any idea on how to make hasMatch match against the entire line? (Setting the delimiter to "\n" or "\0" doesn't work.)

© Stack Overflow or respective owner

Related posts about java

Related posts about scanner