0

I'm having trouble with using jbuttonactionperformed to receive a username and password in java. What is wrong with this code. I searched in google and saw that a character array is used to store getPassword return.Is it wrong?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    char[] input = {'r','o','o','t'};
    if (jTextField1.getText() == "admin" && jPasswordField1.getPassword() == input) {
        srchnm.main(null);

    }

}   
Jithin Sebastian
  • 511
  • 1
  • 6
  • 19

1 Answers1

0

You should never compare Strings and avoid comparing other reference types using ==. Use the equals(...) method for Strings instead, and for the password, the java.util.Arrays class has an equals method you can use.

if (java.util.Arrays.equals(jPasswordField1.getPassword(), input) && 
           "admin".equals(jTextField1.getText())) {
    srchnm.main(null);
}

Alternately, you could change this:

"admin".equals(jTextField1.getText())

to

"admin".equalsIgnoreCase(jTextField1.getText())

Myself (Hovercraft), I like to use String constants so as to avoid debugging typing errors:

// at the top
public static final String ADMIN = "admin";

// ....

// a boolean statement in the if block:
ADMIN.equals(jTextField1.getText())
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373