13

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

4

4 に答える 4

32

「等しくない」は、「not」演算子!と標準で表現できます.equals

if (a.equals(b)) // a equals b
if (!a.equals(b)) // a not equal to b
于 2011-12-03T04:59:43.043 に答える
28

「!」を入れるだけです。ブール式の前に

于 2011-12-03T04:58:49.100 に答える
5
if (!secondaryPassword.equals(initialPassword)) 
于 2011-12-03T04:59:40.003 に答える
1

クラスが同等のものを実装している場合は、次のこともできます

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

を使用しませんが、!特に有用ではなく、読みやすくもありません....

于 2011-12-03T05:35:33.483 に答える