I am having trouble with this method of my class. It always returns false.
Passenger is a separate class. This is a method in my Train class which creates an array list of Passenger objects. I am making a method that will search the ArrayList passengerList for a Passanger object with the name as the parameter.
public boolean search(String a){
Passenger temp;
boolean query = false;
for (int i =0; i<passengerList.size(); i++)
{
temp=passengerList.get(i);
if (temp.getName() == a)
{
query = true;
}
}
return query;
}
should be
String comparison should always use equals() method instead of == (except that strings literals).
if
temp.getName()andaboth not pointing to same object, == condition will fail.==checks for references equality.equals()checks for content equality.This tutorial may help you.