2

I know people have asked this question before, but they didn't use .equals(). So I am going to ask again, why is that I have two strings, but when I compare them with .equals() I get false. The two strings are 1234 (passwordField2.getPassword() and String s = bufferedreader.readLine().) I have used s.toCharArray to compare them, and same thing. I tried printing them both out and I got 1234 1234

Does anyone know why this is happening? Thank you!

user2864740
  • 60,010
  • 15
  • 145
  • 220
Orion31
  • 556
  • 6
  • 28
  • 1
    Whitespace? are their lengths equal? – FatalError Feb 11 '16 at 03:03
  • 5
    Looks like there is an extra space on the second one. Try comparing them after trimming... `passwordField2.getPassword().trim().equals(bufferedReader.readLine().trim());` – Todd Feb 11 '16 at 03:03
  • @Todd I tried to do that, but `.trim()` doesn't work for a `JPasswordField` – Orion31 Feb 11 '16 at 03:06
  • Not `String.equal()` is not equal strings. Find out why: debugger, perhaps. Also, some objects don't support `equals()` in "expected ways", [including Arrays](http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java) (for which it is equivalent to using `arr1 == arr2`). – user2864740 Feb 11 '16 at 03:08
  • Oh, duh, yeah. that returns a `char[]`. Maybe that's why they don't match? – Todd Feb 11 '16 at 03:08
  • @user2864740 didn't help... Also, the JPasswordField returns a char array – Orion31 Feb 11 '16 at 03:11
  • @Orion31 So convert it to something useful. – user2864740 Feb 11 '16 at 03:12
  • @Todd I used s.toCharArray(), and it still outputted `1234`, but when compared I got `false` – Orion31 Feb 11 '16 at 03:12
  • @Orion31 Because `arr1.equals(arr2)` is not the equality you are looking for. Neither is `arr1.equals(str1)` or `str1.equals(arr1)`. The ideal would be `str1.equals(str2)`. – user2864740 Feb 11 '16 at 03:12
  • @user2864740 Char Arrays don't have any functions for converting – Orion31 Feb 11 '16 at 03:13
  • @Orion31 See the `String` constructor overloads. Or, by keeping with character arrays (ick!) use `Arrays.equals(charArr1, charArr2)` per the linked question. Unfortunately, `Object#equals` casts a wide net .. and catches (accepts) values, even when such do not meaningfully apply. – user2864740 Feb 11 '16 at 03:14
  • @user2864740 @Todd Thank you guys! Converting the char array into a string with `String.valueOf()` – Orion31 Feb 11 '16 at 03:18

2 Answers2

5

Looking at the JavaDocs, passwordField2.getPassword() returns a char[].

The following code should work for you:

boolean passwordsMatch = bufferedreader.readLine().equals(
    new String(passwordField2.getPassword())
);

This code works as it converts the char[] to a String that can then be compared to the original String value.

Edit: As Alex L. states in his answer, JPasswordField stores passwords as character arrays for security purposes.

As such, a better way to write this code may be:

boolean passwordsMatch = Arrays.equals(
    passwordField2.getPassword(), 
    bufferedreader.readLine().toCharArray()
);
Community
  • 1
  • 1
Taylor Hx
  • 2,815
  • 23
  • 36
  • And now you've voided the security reason for using char[] over String – MadProgrammer Feb 11 '16 at 04:08
  • 1
    @MadProgrammer I thought it might be out of scope for the question if I dive deep into password best practices in Java. I'm open to any suggested edits if they improve the answer. – Taylor Hx Feb 11 '16 at 04:11
  • Security is never out of scope, neither is correct and professional practices – MadProgrammer Feb 11 '16 at 04:16
  • Thanks @TaylorHx! I also notice that `bufferedreader.readLine().equals(String.valueOf(passwordField2.getPassword()));` works too. – Orion31 Feb 11 '16 at 12:32
2

Combining the other answers with some other points, here are a few issues:

You have extra whitespace in the second password. You shouldn't trim the string, because otherwise leading and trailing whitespace will be ignored, which is bad practice.

Also, JPasswordFields will return a char[] when you call getPassword() (Read why here), and character arrays will never equal strings. The best way (in terms of security) to solve this issue is to store the password as a character array. Then, you can use Arrays.equals(arr1, arr2) to check if the passwords are equal.

Community
  • 1
  • 1
hyper-neutrino
  • 5,272
  • 2
  • 29
  • 50