I am trying to compare two objects from the same class that contains both String and Int parameters.
This is for my lab assignment, and I have tried to use just .equals method without overriding it, did not work, I did some more research on how to compare the String and Int parameters of both objects. So I build a override equals and it only worked with String parameters as I tested each parameter, the Int parameter keeps returning as True, so i am not sure where i am going wrong.
Updated Ok, now i got it working, I'm trying to figure out to return false if Names are different, or if Team names are different. I thought of using if and else but nested if-else?
//This is my equals method to compare the String and Int parameter.
public boolean equals(Object o)
{
Player comp = (Player) o;
if (o == this) {
if (comp.team.equals(this.team)) {
return true;
} else if (comp.jerseyNumber == this.jerseyNumber) {
return true;
}
}else
return false;
}
//And this is the method that calls on Player.
player1.readPlayer();
//Prompt for and read in information for player 2
player2.readPlayer();
//Compare player1 to player 2 and print a message saying
//whether they are equal
if (player1.equals(player2))
{
System.out.println("Same Players");
}else
{
System.out.println("Different Players");
}
So i expected the result to print out as either Same Players if Name, Team and jersey number are the same, or Different Players if all three are different, I think. Instead with all three parameters it prints out as Same player regardless the Int parameter value, when I grayed out the Int Parameter, it actually showed both Same and Different Players result.