1

Eclipse keeps telling me to add a return statement to the method, even though I did so.

public class PrefixCode { 
    public String isOne(String[] words) {
        if(words.length==1) {
            return "Yes";
        }
        ArrayList<Integer> indexPositions= new ArrayList<Integer>();
        for(int i=0;i<words.length;i++) {
            String firstWord=words[i];
            java.util.List<String> listOfWordsToCheck = new ArrayList<String>(Arrays.asList(words));
            listOfWordsToCheck.set(i,null);
            for(int j=0;j<listOfWordsToCheck.size();j++) {
                String secondWord= listOfWordsToCheck.get(j);
                if(firstWord.startsWith(secondWord)==true) {
                    indexPositions.add(j);
                }
                else if(firstWord.startsWith(secondWord)==false);
            }
        }  
        if(indexPositions.size()==0) {
            return "Yes";
        }
        else if(indexPositions.size()!=0) {
            Collections.sort(indexPositions);
            return "No,"+indexPositions.get(0)+"";
        }
    }
}

My return statements are outside of the for loops, so I don't understand what's wrong here.

1
  • 12
    What happens if your if AND your else if conditions resolve to false? What is your method supposed to return in that case? Commented Jan 29, 2014 at 15:50

6 Answers 6

6

There is no default return. The only returns you are making are if some conditions are true. What if the conditions are false?

Add a return after the last else block and you are all good to go.

The else block is redundant. What lies inside the else block should be be without else.

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

Comments

2

Since you have added if, else if, you need to else to that control flow to satisfy the compiler. Logically, size can be either zero or more than zero. So, you need to have if and else part only

     if(indexPositions.size()==0){
         return "Yes";
     } else if(indexPositions.size()!=0){
        Collections.sort(indexPositions);
        return "No,"+indexPositions.get(0)+"";

     } else {
     // return what?
     }

You can simplify this logic by,

if(indexPositions.size() == 0) {
   return "Yes";
} else { //size more than zero
   Collections.sort(indexPositions);
   return "No,"+indexPositions.get(0) + "";
}

1 Comment

there is no 3rd option. just replace "else if" with "else"
2

you should use else instead of

else if(indexPositions.size() != 0) {
    Collections.sort(indexPositions);
    return "No,"+indexPositions.get(0) + "";
}

1 Comment

Why use else block and why not a simple block?
1

The compiler doesn't know if the if conditions are going to succeed. So, you need to add a default return out of those if (even if your if conditions cover all possible cases!)

Comments

0

One of the best practice is to have only one return at the method end! Like this:

public String isOne(String[] words) {
   String isOne = null;

   if(words.length==1){
      isOne = "Yes";
   }

   ...

   if(indexPositions.size()==0){
      isOne = "Yes";
   }
   else if(indexPositions.size()!=0){
     Collections.sort(indexPositions);
     isOne = "No,"+indexPositions.get(0)+"";
   }

   return isOne;
}

Sometimes you may initialize the variable with a default value too, even if in this case is not needed :

 String isOne = "No";

In also in your code there is an "error", the indexPositions.size may only be 0 or more, so you may want to use else instead of else if, and complete the graph. In this case eclipse won't tell you to add a return statement anymore, even if you use the return inside the condition block.

if(indexPositions.size()==0) {
  return "Yes";
}
else {
  Collections.sort(indexPositions);
  return "No,"+indexPositions.get(0)+"";
}

Comments

0

I usually do this by declaring a boolean at the start of the function and set it to false. If for whatever reason the function says that variable is gonna be true. I set the declared variable to true instead of returning true. At the end of the function i return that declared variable.

It then has its default return and if the variable was set to true, it returns true.

Comments

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.