Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

How can I express that two values are not equal to eachother?

Writer Matthew Harrington

Is there a method similar to equals() that expresses "not equal to"?

An example of what I am trying to accomplish is below:

if (secondaryPassword.equals(initialPassword))
{ JOptionPane.showMessageDialog(null, "You've successfully completed the program.");
} else { secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again.");
}

I am trying to find something that will not require me to use if ( a != c).

2

4 Answers

"Not equals" can be expressed with the "not" operator ! and the standard .equals.

if (a.equals(b)) // a equals b
if (!a.equals(b)) // a not equal to b

Just put a '!' in front of the boolean expression

if (!secondaryPassword.equals(initialPassword)) 

If the class implements comparable, you could also do

int compRes = a.compareTo(b);
if(compRes < 0 || compRes > 0) System.out.println("not equal");
else System.out.println("equal);

doesn't use a !, though not particularly useful, or readable....

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy