-3

I have been trying to get user input and based on what they put turn it into a result, every time that i change it. else statement doesn't work, can anybody tell me what i'm doing wrong?

    String heads = "Heads";
    String tails = "Tails";
    
    
    System.out.println("Please choose (H)eads or (T)ails:");
    Scanner in = new Scanner(System.in);
    String inputs = in.nextLine();

    String H = "H";
    String T = "T";
    if(inputs == H) {
        String choice = "0";
    }else if(inputs == T) {
        String choice ="1";
    }else {
        System.out.println("Please try again");
    }
        
  • 8
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – OH GOD SPIDERS Jul 16 '21 at 12:07

1 Answers1

0

In Java, you have to use string1.equals(string2); to compare strings.

In your case:

if(inputs.equals(H)) {
    String choice = "0";
} else if(inputs.equals(T)) {
    String choice = "1";
} else {
    System.out.println("Please try again");
}
Crih.exe
  • 502
  • 4
  • 16