Been racking my brains with this for ages now and it’s just stumped me.
I have a simple string comparison:
public static void login()
{
isIncorrectInput = true;
while (isIncorrectInput)
{
System.out.print("Please enter your password: ");
password = readLine();
if (password.equals(currentUser.password))
{
isIncorrectInput = false;
System.out.print("Successful login!");
}
else
{
System.out.print("Incorrect password. Please try again.\n");
}
}
}
So, ‘password’ is a String variable, currentUser is a instance of a User object which has a password property.
I’ve tried switching which object the method is called on: doesn’t work. Tried ignoring the case: doesn’t work. The passwords are DEFINITELY the same, I’ve stepped through it countless times, it’s just returning false when it should be returning true.
What am I doing wrong!!?
Thanks in advance guys.
Without seeing readLine()’s body I can’t be sure, but it is probably a whitespace issue. Try doing a
trim()on the input before doing the comparison.