1

I have a method that returns something depending on the if conditions. But when I am using the below code, it doesn't recognize my return statement and says to change the method declaration to void. Can you please help optimizing the code.

if(myList == null){
    return abc;
} else {
    for(myList myListItem : myList){            
        if(myList.getId() != null){
          if(TEST1.equals(myListItem.getId())){
            return abc;
          } else if(TEST2.equals(myListItem.getId())){
            return xyz;
          } else if(TEST3.equals(myListItem.getId())){
            return pqr;
        }
    }  
}
}
1
  • 1
    What return value do you expect if the list referred to by myList is empty? Commented Oct 26, 2016 at 17:39

4 Answers 4

1
if(myList == null){
    smth = abc;
} else {
    for(myList myListItem : myList){            
        if(myList.getId() != null){
          if(TEST1.equals(myListItem.getId())){
            smth = abc;
          } else if(TEST2.equals(myListItem.getId())){
            smth = xyz;
          } else if(TEST3.equals(myListItem.getId())){
            smth = pqr;
        }
    }  
 }
   return smth;
    }

Try this it will work for your function with different conditions

Sign up to request clarification or add additional context in comments.

1 Comment

Be careful with this. Its return value is based on the LAST value in the loop whose id matches TEST1, TEST2 or TEST3; whereas the return value of your original code (if it were made to compile) would be based on the FIRST such value in the loop.
1

For your else you are not returning anything... return something there or return something outside your if:

          else {
                for(myList myListItem : myList){            
                    if(myList.getId() != null){
                        if(TEST1.equals(myListItem.getId())){
                            return abc;
                        } else if(TEST2.equals(myListItem.getId())){
                            return xyz;
                        } else if(TEST3.equals(myListItem.getId())){
                            return pqr;
                        }
                    }  
                }
           **return something;**//<--here
           }
//or return here
return something;//<-- OR HERE

Comments

0

The compiler is simply telling you that there are paths within your code that would not see a return statement!

Your very first if in the for-block does not have an else; thus there is no return statement when that thing is null! And then - what happens if the for loop doesn't loop at all?!

In other words: believe your compiler. When it tells you, that your method "should be void", then it means: there is at least one path left that doesn't return a value. It won't stop complaining until you fixed all of those paths!

Finally: you might want to read about the single layer of abstraction principle. You see - you created code that is so hard to read that you didn't see that obvious problem. So you could step back now and learn how to write readable code ...

Comments

-1
if (myList != null) {
  for() {}
}
return abc;

2 Comments

two possibilities one is return outside if or add a return to else
At least, please add some explanation to your code. As it is, it doesn't even compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.