-1

I wrote a simple code just to check the values between textfields and compared whether they are the same or not. I want them to be the same, if not it will produce an error. It's about rewriting an email.

    String a = studentemail.getText();
    String b = rewritestudentemail.getText();

    if(a != b){
         JOptionPane.showMessageDialog( null, "Student Email rewritten incorrectly.","Error!",JOptionPane.OK_OPTION);

    }

The program persists that there is an error even though I indicated the same string values in both of the fields. Why's that?

SANN3
  • 9,459
  • 6
  • 61
  • 97
Shinji
  • 1,799
  • 2
  • 11
  • 13

3 Answers3

0

Try:

if (!a.equals(b)) {
   ...
eKek0
  • 23,005
  • 25
  • 91
  • 119
0

Change your conditional to be this:

if(!a.equals(b))
{
   JOptionPane.showMessageDialog( null, "Student Email rewritten
   incorrectly.","Error!",JOptionPane.OK_OPTION);
}

Make sure you have the ! before a.equals(b)) since you only want the error to appear when they are not equal.

Mike Koch
  • 1,540
  • 2
  • 17
  • 23
0

rather layout your logic like this:

if (!a.equals(b)){
 //JoptionPane...
 }

In java two strings are compared by using the ".equals()" function.