Reputation: 348
I want to exclude any characters except letters and numbers but when I run my program no matter what I enter I always enter the while loop.
System.out.println("Please enter the name of the next dancer");
while(!scan.hasNext("^[a-z0-9]+$/i"))
{
System.out.println("Please ensure you only enter letters of the alphabet and numbers bewteen 0 and 9");
scan.nextLine();
}
arrCelebs[celeb] = scan.next();
For example I enter "a" I get - System.out.println("Please enter the name of the next dancer");
And the same if I enter "%"
Upvotes: 1
Views: 62
Reputation: 785128
/i
won't make it ignore case in Java, use:
"(?i)^[a-z0-9]+$"
Or simply:
"^[a-zA-Z0-9]+$"
Another problem in your approach is that by default ^
matches start of text, not start of line, so if your first input will not match regex, then next one can't match it too since it will not be at start of text. So either make your anchors match start of line with multiline flag (?m)
or simply remove ^
.
Upvotes: 2