java - why cant i get out of this while loop? -
this question has answer here:
- how compare strings in java? 23 answers
our project involves reading files premier league , returning 1. outcomes of games played, 2. fixtures yet played , 3. leaderboard(table). i'm displaying menu in command line , taking in user input there. i'm using while loop match userinput output display wanted if don't enter 1,2 or 3 there's error , display menu again , ask them enter input. if enter 1,2 or 3, doesn't display results can't out of while loop. can me? 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; } }
if want compare string value, should use equals()
:
if (userchoice.equals("1")) { // }
Comments
Post a Comment