Java if statement does not apply

I have a perfectly working app that is coded in java. But one if statement does not want to work... :(

login.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String usernamed = nameField.getText();
            String passwordd = String.valueOf(passwortField.getPassword());

            System.out.println(usernamed);
            System.out.println(passwordd);

            if (usernamed == "user") {
                if (passwordd == "pass") {
                    dispose();

                    JFrame tickets = new tickets(usernamed);
                    tickets.setSize(1000, 700);
                    tickets.setVisible(true);
                    tickets.setTitle("Tickets - Start");
                }
            } else {
                error.setText("Wrong Password/ Username");
            }
        }

    });

When i write "user" in the JTextField nameField, "pass" into the JPasswordField passwortField and click on the JButton login, It should open a new Window but it doesn`t and JLabel error gets set to: "Wrong Password/ Username". But in the Console i get the Outprint:

user
pass

What am i doing wrong?

1 answer

  • answered 2017-11-12 20:13 Damian Lattenero

    Use equals here my friend,

    password.equals( "pass")
    

    And here

    username.equals("user")
    

    And in all the places you are comparing strings with ==

    Strings in Java are objects, if you use == you are checking the reference to the object, not the string inside