3

I ave a code where I want to use != sign. But since I am using String, How do I not equals Sign. Hers is my code. So I want all this statement not to equal to each other so it can print Tie Game.

    if (Array[0] == Array[currentPlayer] &&  Array [1] == 
    Array[currentPlayer] && !Array [2] == Array[currentPlayer])

The above code is when everything equals to each other. But I want this statements not to equal each other.

Keep in mind that I have not used Int or Char, I am using String.

Jainam Patel
  • 41
  • 2
  • 3
  • 12

2 Answers2

8

For string inequality, negate a call to the equals method using the !:

String x = "ABC";
String y = "XYZ";
if(!x.equals(y)) {
    //do stuff
}

! can be used to negate ANY boolean expression, and String.equals returns a boolean.

Brian
  • 3,091
  • 16
  • 29
1

You can do something like:

if (!Array[0].equals(Array[currentPlayer]) &&  !Array[1].equals(Array[currentPlayer])  
    && Array[2].equals(Array[currentPlayer]))

Use equals() if you want case sensitive match meaning it will look at case of string as well when matching. If you want case insensitive matching you can use equalsIgnoreCase() method in place of equals()

hitz
  • 1,100
  • 8
  • 12