I am writing a login script for a game that i have made. I am currently in the process of checking the information given to make sure it is valid. I have run into the problem that when I go to check to see if 2 text fields have the same value. When they do they do the opposite of what i want it to do.
private void regAccConfEmailFieldFocusFocusLost(FocusEvent event) {
if(regAccConfEmailField.getText() == regAccEmail.getText() && regAccConfEmail != null)
{
regAccConfEmailField.setBorder(new LineBorder(Color.green, 1, false));
confEmail = true;
}
else
{
regAccConfEmailField.setBorder(new LineBorder(Color.red, 1, false));
confEmail = false;
}
}
private void regAccConfSecQFieldFocusFocusLost(FocusEvent event) {
if(regAccConfSecQField.getText() == null)
{
regAccConfSecQField.setBorder(new LineBorder(Color.red, 1, false));
secQuestion = false;
}
else
{
regAccConfSecQField.setBorder(new LineBorder(Color.green, 1, false));
secQuestion = true;
}
}
This is the code that i have, and i need to know why each of these methods does the opposite of what it is given.
Say that regAccConfEmailField and regAccEmailField both equal hello@gmail.com
It will go to the if statement and not the else. If needed, i can provide more code.
There are 2 issues with this statement:
nullcheck first so that it short-circuits the expression ifregAccConfEmailisnullString.equalsto compareStringcontent instead of the==operator. The==operator is used to compare object references and is currently giving you the opposite of what you want as the values from the 2 fields will be differentStringobjects.You can replace with
Also
regAccConfSecQField.getText()can never benullfrom aJTextFieldso replaceif (regAccConfSecQField.getText() == null)
with
FocusListenerwhich relys onFocusEventsfor performing the validation. Have a look at using an DocumentListener for triggering validation on document changes.