0

I'm working on a login screen for my game, and everything works fine, except for one thing. When I check to see if the text from the JTextField == "", it still launches the game and whatnot.

Here's a look at my code:

public void attemptLogin(String username, String passcode) {
    if (username != "" && passcode != "") {
        System.out.println(username);
        System.out.println(passcode);
    } else if (username == " " || passcode == " ") {
        JOptionPane.showMessageDialog(null, "Put in your credentials!", "Hey you!", JOptionPane.ERROR_MESSAGE);
    }
}

}

In my main class, it goes:

public void actionPerformed(ActionEvent action) {
        if (action.getSource() == login) {
            gamelogin.attemptLogin(username.getText(), passcode.getText());
        } else if (action.getSource() == register) {
            account.registerAccount();
        }
    }

Now what must happen is that if the JTextField comes up blank, then show a JOptionPane and if not, login to the game (added later), but evidently, this is not happening. It just outputs white space in the console.

Thanks in advance! :)

mKorbel
  • 109,525
  • 20
  • 134
  • 319

1 Answers1

0

You need to use the equals-method for comparing strings. Neither of your comparisons will ever be true at the moment.

username.equals("") && passcode.equals("")
Zavior
  • 6,412
  • 2
  • 29
  • 38