error coming while compare two textbox values using java netbeans -


when compare 2 values in jtextfield coming error when space including showing correct. without using showing error. example in jtextfield1 "abcd" when in jtextfield2 "abcd " show message correct else showing error.

but want show if jtextfield1 showing "abcd" , same in jtextfield2 same "abcd" show message correct else false in jlabel.

my code follows

 private void jtextfield2keypressed(java.awt.event.keyevent evt) {                                            // todo add handling code here:       if (jtextfield1.gettext().equals(jtextfield2.gettext()))     {         jlabel1.settext("sucess");     }     else     {        jlabel1.settext("fail");    }    } 

when space including showing correct.

use avoid spaces

if (jtextfield1.gettext().trim().equals(jtextfield2.gettext().trim()))     

from doc public string trim()

returns copy of string, leading , trailing whitespace omitted. if string object represents empty character sequence, or first , last characters of character sequence represented string object both have codes greater '\u0020' (the space character), reference string object returned.

otherwise, if there no character code greater '\u0020' in string, new string object representing empty string created , returned.


Comments