Check password fields with each other

Asked

Viewed 733 times

0

How do I validate two password fields if both are equal?

if(pfSenha.getPassword() == pfCSenha.getPassword()) {
    JOptionPane.showMessageDialog(null, "Senhas conferem!");
} else {
    JOptionPane.showMessageDialog(null, "Senhas não conferem!");
}

I’ve tried to...

if(new String(pfSenha.getPassword()) == new String(pfCSenha.getPassword())) {
    JOptionPane.showMessageDialog(null, "Senhas conferem!");
} else {
    JOptionPane.showMessageDialog(null, "Senhas não conferem!");
}

How can I solve?

1 answer

2


The problem with your code is the operator ==.

In Java == compares whether two objects point to the same reference (only for non-primitive types, of course). In your case, you should use the method .equals()

if(new String(pfSenha.getPassword()).equals(new String(pfCSenha.getPassword()))) {
    JOptionPane.showMessageDialog(null, "Senhas conferem!");
} else {
    JOptionPane.showMessageDialog(null, "Senhas não conferem!");
}

Behold: What’s the Difference between ". equals" and "=="?
How to compare Strings in Java?

  • 1

    My fault. Edition accepted =). Glad you could work it out

  • Me? You helped me solve! Thank you very much champion!! Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.