Click here to Skip to main content
15,886,792 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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.
For example,
Java
Array[0] == Array [currentPlayer] && Array[1] == Array [currentPlayer] 

&& Array[2]== Array [currentPlayer]  doesnot equal Array[0] == Array [currentPlayer]

&& Array[1] == Array [currentPlayer] && Array[2]== Array[currentPlayer]
then print Tie Game.


Keep in mind that I have not used Int or Char, I am using String.
Posted
Comments
Maciej Los 18-May-15 14:54pm    
Why don't you use classes and ArrayList?
Maciej Los 18-May-15 15:04pm    
https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

Have a look here: Comparing Strings and Portions of Strings[^]

As i mentioned in comment, you have to operate on data, not on strings. Java is really powerful programming language. It enables to create custom classes, such as

Java
//definition
public class Player{
    public String Name;
    public Int Score;

    public Player(String n, Int s){
        Name = n;
        Score = s;
    }

    public toString() {
        return Name + "'s score is: " + Score.toString();
    }
}

//Implementation:
ArrayList<player> players = new ArrayList<player>();
players.Add(new Player("Adam", 50));
players.Add(new Player("Eve", 55));

//other stuff...
Player player1 = players.get(0);
Player player2 = players.get(1);
if(player1.equals(player2)){
//two objects are equal on Name and Score
}


Do you see the difference?

For further information about comparing objects in Java, please see: Java Programming: Comparing Objects[^]
 
Share this answer
 
v2
You use the equals method.

Java
if (!someVariable.equals("Bob"))
    System.out.println("Is not Bob!");
else
    System.out.println("Is Bob!!");


Check out this link for more details:
Java - String equals() Method[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900