I am trying to search through an arraylist and return an object. I have a Vehicle class in which I have a method named search and as parameter the registration number and arraylist are passed as parameters.
However the problem is that when going through the for each the system is never trying to compare the 2 Strings (reg numbers) and therefore the method is always returning an empty object.
Method:
public Vehicle search(String id, List<Vehicle> myList) {
Vehicle currenctVeh = new Vehicle();
for(Vehicle v: myList)
{
if(v.regNo == id)
{
currenctVeh = v;
}
}
return currenctVeh;
}
Being Called:
Vehicle searchVeh = new Vehicle();
String regNum = JOptionPane.showInputDialog(null, "Enter Vehicle Registration Number");
searchVeh.search(regNum, allVehicles);
System.out.println(searchVeh.toString());