Hello all I am trying to write a loop in my code that would prompt user if they enter something other than what I have predefined. I am somewhat familiar with this done to user input that is not specific word or int but not sure when user has three choices to choose from.
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
// this class will instantiates player object
public class Adventure {
public static void main(String[] args) {
// main method
System.out.println("Hello and welcome to my text adventure! ");
Scanner myinput = new Scanner(System.in);
Player gameplayer = new Player(); // create a player object and assign it to gameplayer
System.out.print("Please enter your name.\n");
String nameofPlayer = myinput.nextLine();
gameplayer.setPlayer(nameofPlayer);
System.out.print("Please enter your class. (Mage, Archer, Warrior)\n");
List<String> names = Arrays.asList("mage","archer","warrior");
String userinput;
while (myinput.hasNext()) {
userinput = myinput.nextLine();
String nameofClass = userinput.toLowerCase();
if (!names.contains(nameofClass)) {
System.out.println("I'm sorry, what was that?");
} else {
gameplayer.setclassName(nameofClass);
System.out.println("Hello " + gameplayer.getPlayer() + " the "
+ gameplayer.getClassName()+ ". What health do you have?");
}
}
int healthofPlayer ;
while (myinput.hasNextInt()){
healthofPlayer = myinput.nextInt();
if ((!myinput.hasNextInt())){
System.out.println("I'm sorry, what was that?");
}
else {
gameplayer.setHealth(healthofPlayer);
System.out.println("Very good. Now let's get started on your adventure.");
System.out.println("You awake alone, disoriented, and locked in the CS1331 TA Lab.");
}
return;
}
}
}