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

无人久伴 提交于 2019-11-30 01:48:11

问题


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).


回答1:


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




回答2:


"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



回答3:


if (!secondaryPassword.equals(initialPassword)) 



回答4:


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....



来源:https://stackoverflow.com/questions/8365630/how-can-i-express-that-two-values-are-not-equal-to-eachother

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!