-2

I don't know what's going on with it. Code below. I'm not trying to get anyone to code the whole thing for me, just don't know what's wrong and would like a little help

private void javabutton1(java.awt.event.ActionEvent evt) {
    String testa= new String (jPasswordField2.getPassword());
    String testb= new String (jPasswordField3.getPassword());
    if (testa.toString() == testb.toString()){
        JOptionPane.showMessageDialog(this, "Success");
    }
}

When I replace testa.toString() == testb.toString()) with "A" == "A". The messagebox "Success" is achieved but this entry comparison won't work

Also: The text entered in both jPasswordField2 and jPasswordField3 are the same.

takendarkk
  • 3,347
  • 8
  • 25
  • 37

6 Answers6

1

You should try:

testa.equals(testb)

And there is no point of doing this:

String testa = getSomething();
String temp = testa.toString();
// becasue
testa.equals(temp) // always true

If you would have something like:

String a = getSomething();
String b = a;
a == b // now this is true, because they have the same reference/pointer
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
0

Use .equals() when comparing strings.

ltalhouarne
  • 4,586
  • 2
  • 22
  • 31
0

Basically, you should never use == to compare Strings, instead use equals(), so for you:

testa.equals(testb)

The difference is that == is used to compare references, it is saying: "Do these two String references point to the same String object in memory?"...this is unpredictable due to how Java stores Strings, which basically accounts for why "A" == "A" returns true...not something to go into here.

The equals() method is more what you would expect, in the Java Class String, this method basically checks whether or not each character in the String is the same, and returns true if they do.

If it's an object, you should use equals() to compare, if its a primitive data type, such as an int (or you are checking if a reference is null) then use ==.

Ben
  • 346
  • 1
  • 8
0

Try with String.equals()

Consider two different reference variables str1 and str2

str1 = new String("abc");
str2 = new String("abc");

if you use the equals()

System.out.println((str1.equals(str2))?"TRUE":"FALSE");

You will get the output as TRUE

if you use ==

System.out.println((str1==str2)?"TRUE":"FALSE");

Now you will get the FALSE as output because both str1 and str2 are pointing to two different objects even though both of them share the same string content. It is because of new String() everytime a new object is created.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

Fix your issue by using String.equals(string) like this

String testa= new String (jPasswordField2.getPassword());
String testb= new String (jPasswordField3.getPassword());
    if (testa.equals(testb)){
         JOptionPane.showMessageDialog(this, "Success");
     }
Visionstar
  • 355
  • 2
  • 12
0

While I agree with Takendarkk that answer duplicate questions promotes their repeated posting, I think there is at least one issue that should be noted which has not been mentioned. StephenTG asked a poignant question in the comments: "Why do you need to convert your Strings to Strings?"

Given the name of your variables, if you are indeed using the swing JPasswordField, then the getPassword() method returns a char[] array. You don't need to convert this to a string, you can compare them using java.utils.Arrays#equals(char[]. char[]) to get the result you desire. Your code might look like this:

private void javabutton1(java.awt.event.ActionEvent evt) {
    char[] testa = jPasswordField2.getPassword();
    char[] testb = jPasswordField3.getPassword();
    if (Arrays.equals(testa, testb)){
        JOptionPane.showMessageDialog(this, "Success");
    }
}
Paul Richter
  • 10,908
  • 10
  • 52
  • 85