0

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?

tehlexx
  • 2,773
  • 15
  • 29
user2792920
  • 9
  • 1
  • 6

4 Answers4

4

The problem is that if you don't find a match then you don't return anything. Try this:

private Unit getUnitObject(String unit1Code) {
    for (int i = 0; i < units.size(); i++) {
        Unit currUnit = units.get(i);
        String unitCode = currUnit.getUnitCode();

        if (unit1Code.equals(unitCode)) {
             return currUnit;
        } 
    }
    return null;
}

Notice that I'm comparing String objects using .equals() as well. You may want to return something better than null if nothing matches too.

telkins
  • 10,280
  • 8
  • 48
  • 76
0

The problem is that you only return something when your if-statement comes true, but you need to return also something when it is false. It is good practive to only have one return-statement in a class. Create a variable of the return type at the beginning (instantiated with null or a standard value) and change this variable based on your conditions. In the end return that variable.

  private Unit getUnitObject(String unit1Code) {
    Unit selectedUnit = null;
       for (int i = 0; i < units.size(); i++) {
       Unit currUnit = units.get(i);
       String unitCode = currUnit.getUnitCode();


        if (unit1Code == unitCode) {
          selectedUnit = currUnit;

           } 
         }
       return  selectedUnit;
       }
Mirco
  • 2,713
  • 3
  • 34
  • 53
0
    private Unit getUnitObject(String unit1Code) {
    Unit selectedUnit = new Unit();
    for (int i = 0; i < units.size(); i++) {
    Unit currUnit = units.get(i);
    String unitCode = currUnit.getUnitCode();

     if (unit1Code.compareTo(unitCode)) {
      selectedUnit = currUnit;

        } 
      }
       return selectedUnit;
    }

You have to always return a Unit object, not only the the uni1Code is equals to UnitCode. Thus, you create a variable in the begining of the method and make the assigment if the if is true, and return in the end. It will return empty Unit or the currentUnit..

X-Pippes
  • 1,142
  • 7
  • 24
0

With this configuration I hope it will work

 private Unit getUnitObject(String unit1Code) {
        Unit selectedUnit=null;
        for (int i = 0; i < units.size(); i++) {
            Unit currUnit = units.get(i);
            String unitCode = currUnit.getUnitCode();

             if (unit1Code.equals(unitCode)) {
              selectedUnit = currUnit;

            } 
        }
        return selectedUnit;
    }
justadeveloper
  • 527
  • 10
  • 25