mercredi 4 mars 2020

How come this If statement isn't comparing 2 variables properly? [duplicate]

I'm currently learning Java and was practicing with user inputs and wrote this code:

static void loginMenu() {
    //Set password
    String pw = "abc123";
    //Create a scanner object
    Scanner inputScanner = new Scanner(System.in);

    System.out.println("Enter password to continue: ");

    //Set the scanner object to watch for the next string input
    String loginMenuInput = inputScanner.nextLine();

    if (loginMenuInput == pw) {
        System.out.println("Password correct... loading menu");
    }
    else {
        System.out.println("Password incorrect");
    }
}

But whenever I run said code it outputs "Password incorrect". I've double checked inside the debugger to make sure that the Input is the exact same as the "pw" variable.

Does anyone know why this isn't working?

Aucun commentaire:

Enregistrer un commentaire