lundi 18 juillet 2016

New to Java - Validate String word and other issues

I've been reading validation for user input is not a number or if input isn't a character, but how do i check if the user input is something i defined for them?

 System.out.println("What meal would you like to eat?"
        + " (appetizer, soup, salad, main, or dessert)");
    String meal = console.next();

I saw

Scanner sc = new Scanner(System.in);
System.out.println("Please enter a vowel, lowercase!");
while (!sc.hasNext("[aeiou]")) {
    System.out.println("That's not a vowel!");
    sc.next();
}

but compiler does not like when i change

(!sc.hasNext("[aeiou]")) 

to

(!sc.hasNext("[appetizer, soup, salad, main, dessert]"))

Aucun commentaire:

Enregistrer un commentaire