1

Actionlistener code

Player p = new Player();
/**
** Other code here
**/
private static JLabel Status;
final JTextField username = new JTextField(6);
loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
           data = username.getText();
                if(p.Login(data)){
        Status.setText(data+" is correct");
    } else {
        Status.setText(data+" is wrong");
    }
         }
      });

Player class code

  public boolean login(String username){
    if(username == "adam"){
        return true;
    } else {
    return false;
    }
  }

I am always getting that username is wrong no matter what input there is, what should I do? what am I doing wrong?

fat santa
  • 25
  • 7

2 Answers2

2

You must use equals() not == for comparing your Strings.

Note: == checks the reference to the object are equal .

Note: equals() This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object

Source for equal function

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
2

Your error is in:

public boolean login(String username){
    if(username == "adam"){
        return true;
    } else {
        return false;
    }
}

You need to change:

if(username == "adam"){

To:

if(username.equals("adam")){

This is a classic String comparison mistake. Hope this helps :)

Stephen Buttolph
  • 643
  • 8
  • 16