I'm a bit stumped by this as I am still pretty new to Java.
Here is my issue:
I need to return an object by using a string to pass it to another object. Ie, I want to pass a string to the function (getObject
in this case) and then compare it to an ArrayList
of unit objects by using their getCode
function.
What I have so far:
private Unit getUnitObject(String unit1Code) {
for (int i = 0; i < units.size(); i++) {
Unit currUnit = units.get(i);
String unitCode = currUnit.getUnitCode();
if (unit1Code == unitCode) {
Unit selectedUnit = currUnit;
return selectedUnit;
}
}
}
It gives me an error - "this method must return a result of type unit" I tried moving the return out of the for loop and still had no success? Can I do it this way?