-1

I'm confused as to why I can't step into either of these if statements. Am I missing something incredibly simple? I'm typing in aa or AA or c or C and it doesn't step into either if statement. Any ideas why?

while(play){
        String answer;
        Scanner input = new Scanner( System.in );
        System.out.println("Please select an option");
         answer = input.nextLine();
         answer = answer.toUpperCase().trim();
         System.out.println(answer);

     if(answer == "AA"){
         System.out.println("Have fun!");
         System.out.println("Enter player 1's name");
         Player player1 = new Player(input.nextLine());
         System.out.println("Enter player 2's name");
         Player player2 = new Player(input.nextLine());

         System.out.println(player1.getName());
         System.out.println(player1.getCash());
     }

     else if (answer == "C"){
         play = false;
         System.out.println("Thanks for playing");
     }



}
Cody Krauskopf
  • 330
  • 1
  • 4
  • 13

1 Answers1

0

For comparing string you need to use equals method. Example:

if(answer.equals("AA")){

Note this will compare case-sensitive. If you don't want case-sensitive use:

if(answer.equalsIgnoreCase("AA")){
brso05
  • 13,142
  • 2
  • 21
  • 40