3

I'm working on a project with a registration form. The form asks the user to enter a password of their choice in a JPassword Field, and to enter it again in another JPassword Field.

I'm using a JOptionPane to prompt the user if the passwords do not match. but when i use passwordField.getPassword().toString() on the two, they don't match. I have tried entering a basic "12345" for example on both but i still have no luck.

i know you should use .equals(), but what is the equivalent to this for "not equals" without using the "!=" operator.

here is the following code.

    if(e.getSource() == submit)
    {   
        String name = nameTextField.getText();
        String address = addressTextField.getText();
        String phoneNumber = numberTextField.getText();
        String dob = datePicker.getDateFormatString();
        String password = passwordField.getPassword().toString();
        String password2 = passwordFieldTwo.getPassword().toString();


        try
        {
            //If the user leaves the field empty
            if(name == null || address == null || phoneNumber == null || dob == null 
                || password == null || password2 == null)
            {
                //Error message appears to prompt the user to 
                //complete the form
                JOptionPane.showMessageDialog(null, "All fields must be complete to submit.", "Woops", JOptionPane.ERROR_MESSAGE);
            }

            if(password != password2)
            {
                    JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
                    passwordField.setText(null);
                    passwordFieldTwo.setText(null);
            }

Any help with this would be much appreciated.

Aaronward
  • 109
  • 2
  • 11
  • `passwordField.getPassword()` returns a char array .calling `toString()` doesn't give you password as a string. you can convert to string by `new String();` but you shoudn't .there is a reason for return it as a `char array` instead of `String` .read this question http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java – Madhawa Priyashantha Mar 22 '16 at 15:19
  • As to your edit after the question got marked as a duplicate: **Just use `!` on the result of `.equals()` to get a behavior alike to `!=`.** – Matt Mar 22 '16 at 15:44

1 Answers1

1

The != related to equals() for String comparison is expressed by !x.equals(y)

As an example lets take your code, to see if the two passwords not match do the following:

if (!Arrays.equals(passwordField.getPassword(), passwordFieldTwo.getPassword())) {
   JOptionPane.showMessageDialog(null, "Passwords do not match.", "Woops", JOptionPane.ERROR_MESSAGE);
} 
M. Suurland
  • 665
  • 11
  • 30
  • 1
    This worked for me. used following to ensure that the passwords matched and also were not left null if (!Arrays.equals(passwordField.getPassword(), passwordFieldTwo.getPassword()) && passwordField.getPassword() != null && passwordFieldTwo.getPassword() != null) – Aaronward Mar 22 '16 at 16:17
  • @Aaronward good to hear, can you accept my answer and upvote it? Then other people who see this question know this answer is correct – M. Suurland Mar 22 '16 at 16:27