0

our project basically involves reading files about the premier league and returning 1. outcomes of games played, 2. fixtures yet to be played and 3. the leaderboard(table). I'm displaying a menu in the command line and also taking in the user input from there. I'm using a while loop to match the userinput with what output to display but I wanted to say if they don't enter 1,2 or 3 that there's an error and to display the menu again and ask them to enter another input. However now if I do enter 1,2 or 3, it doesn't display my results I can't get out of the while loop. Can anyone help me? This is my code!

while(userInput == false)
    {
        Scanner input = new Scanner(System.in);
        String pattern = "[1-3]{1}";
        String userChoice;
        System.out.println(menuOptions);
        userChoice = input.nextLine();
        if(userChoice == "1")
        {
            System.out.print(outcomesResults);
            userInput = true;
        }
        if(userChoice == "2")
        {
            System.out.print(fixturesResults);
            userInput = true;
        }
        if(userChoice == "3")
        {
            System.out.print(leaderboardResults);
            userInput = true;
        }
        else if(!userChoice.matches(pattern))
        {
            System.out.println();
            System.out.println("Error: unidentified selection. Please choose 1,2 or 3");
            userInput = false;
        }
    }
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

1

If you want to compare the String value, you should use equals():

if (userChoice.equals("1")) {
   // do something
}
Doon
  • 3,339
  • 5
  • 25
  • 31