-1

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;
            }



            }

}
BabyC0d3eR
  • 27
  • 8

2 Answers2

-1

There's no need loop for this. It'll be easier to delegate to a function as you'll see.

 List<String> acceptable = Arrays.asList("mage", "archer", "warrior");

 System.out.print("Please enter your class. (Mage, Archer, Warrior)\n");

 gameplayer.setclassName(promptFor(acceptable));


 // having a function for this encapsulates the looping and the checking
 // as well as the reprompting - it also means we can leave the loop
 // with the right answer
 String promptFor(Set<String> acceptable) {
     while(true) {  // while true sucks, but you've asked for an indefinite loop
         String next = myinput.next().toLowerCase();
         if (acceptable.contains(next)) { 
             return next;
         }

         // so it was a bad input
         System.out.println("I'm sorry, what was that?");
     } // loop some more

     return null; // unreachable code
 }
Ashley Frieze
  • 3,757
  • 1
  • 19
  • 19
-1

try out this, it should work out the rest.

public static void main(String[] args) {
// main method

System.out.println("Hello and welcome to my text adventure! ");
List<String> names = Arrays.asList("mage","archer","warrior");
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");
String userinput;
while (myinput.hasNext() || myinput.hasNextInt()) {
    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 numberofHealth = myinput.nextInt();
    }
    System.out.println("Very good. Now let's get started on your adventure.");
    gameplayer.setHealth(numberofHealth);
    return;
   }
}
Seek Addo
  • 1,791
  • 2
  • 16
  • 30
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/113943/discussion-on-answer-by-seek-addo-how-to-validate-string-from-user-input-to-pre). – Brad Larson Jun 06 '16 at 16:06