0

Below is a snippet of code from a class in a simple program I'm writing that plays a simple game of Blackjack. I don't understand why the hit method isn't executing whenever, upon execution of the program, I enter "hit". It goes to the else part of the statement every time regardless of what I enter. I even added a System.out.println statement to make sure that the strings matched. I feel like I must be making a very basic mistake but I just can't seem to figure it out.

System.out.println("Would you like to hit or stand?");
    Scanner input = new Scanner(System.in);
    String playerDecision = input.nextLine();
    //System.out.println(playerDecision);

    if(playerDecision == "hit") {
        hit();
    }
    else { System.out.println("ERROR");

    }

}

public void hit(){
    player.makeHand(deck.draw());
    System.out.println("You have the following  cards: ");
    player.getHand();
    System.out.println("Your hand total is ");
    System.out.println(player.findHandTotal());

}

landy1027
  • 63
  • 10

1 Answers1

0

Wrong string comparison. Try

if("hit".equals(playerDecision)) {
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78