[Solved] Loop not ending


The while loop will continue as long as temp is true. In the nested if-statement we set temp as false which exits the loop.

As for the playerPick: the variable is declared outside of the loop so It should be accessible anywhere within the function that is below the declaration (code is read top down here). If you are trying to access playerPick from another method then you will need to declare the variable in the super class.

Note: I also cleaned up your if-statement to make it more readable.

Hope this helps!

System.out.println("Please choose either Applaro, Svartra, Tunholmen, or Godafton.");
boolean temp = true;
String playerPick = "";
    while (temp) {
        playerPick = console.next();
        if (playerPick.equals("Applaro") || playerPick.equals("Svartra") || playerPick.equals("Tunholmen") || playerPick.equals("Godafton")) {
            System.out.println("You have picked " + playerPick);
            temp = false;
        }
        else
            System.out.println("Please enter a valid input.");
    }

0

solved Loop not ending