Home > Blockchain >  Missing return statement but not sure how to fix
Missing return statement but not sure how to fix

Time:11-04

I'm currently taking an online intro to java course and I'm stuck on this method. The goal is to provide a search function in a pretend digital store, where the returned value in this method is referenced in another method to provide a product ID for a search term. If the search term doesn't match anything from the products array, I specifically need to return -1. When I try to run it I get an error saying that this method is missing a return statement. Can anyone help me understand why my return statement isn't working? Do I need to provide more info?

String[] products = {...}; //instance variable

    public int findProduct(String searchText) {
        for (int i = 0; i < products.length; i  ) {
            String product = products[i];
            if (searchText.equals(product)) {
                return i;
            } else {
                return -1;
            }
        }
    }

CodePudding user response:

remove the else statement and move the return -1 outside of the for loop.

return statement will stop the for loop which is probably not what you want

CodePudding user response:

You want the -1 outside of the loop, because you wannt return it at the end if no element has matched, in that way you will return -1 after the first element that doesn't match gets evaluated.

  public int findProduct(String searchText) {
        for (int i = 0; i < products.length; i  ) {
            String product = products[i];
            if (searchText.equals(product)) {
                return i;
            } 
        }
        return -1;
    }

What you did would work in Go with yield tho ;)

CodePudding user response:

It's because java doesn't now if products.length() is equals to 0 until runtime. So, if it's 0 any of the return sentences is executed because the for loop doesn't make any iteration.

Then the IDE tells you that you need to add a return statement in case of products.length() returns 0.

CodePudding user response:

Your function is running a loop. What would happen if the loop ends? Think about this example

findProducts("");

If I call that function with an empty string as an argument, it will skip the loop and then... well...

There is no return after that, and that would simply be an error in Java, since your function is telling the compiler it will always return an int. So your compiler detects there is probably a missing return statement and that what that error is trying to say

A simple solution would be to add another return AFTER the loop so that you can handle those cases as well, and you guarantee that you are always returning an int. Probably something like this:

    public int findProduct(String searchText) {
        for (int i = 0; i < products.length; i  ) {
            String product = products[i];
            if (searchText.equals(product)) {
                return i;
            } else {
                return -1;
            }
        }
        return -1;
    }

And there you go. Remember, if you declare your function with a particular return type, it should ALWAYS return something of said type. Otherwise, you will get an error

CodePudding user response:

When you creating an function, make sure you insert your return value first to keeping track.

for loop will not stop your cursor to reach the bottom of the function.

public int findProduct(String searchText) {
    for (int i = 0; i < products.length; i  ) {
        String product = products[i];
        if (searchText.equals(product)) {
            return i;
        } 
    }
  return -1;
}

Rather than inserting your compared value to temp, I suggest iterate the array directly further read

https://www.geeksforgeeks.org/check-if-a-value-is-present-in-an-array-in-java/

  • Related