0

This method is supposed to return true if one of the first 4 elements in the array is a 9. The array length may be less than 4. In my method, for some reason, I keep getting "missing return statement error".

public boolean arrayFront9(int[] nums) {
   if (nums.length < 4) {
    int counter = 0;
     while (counter != nums.length) {
      if (nums[counter] == 9) {
        return true;
      }else{
        counter = counter + 1;
      }
    } if (counter > nums.length) {
      return false;
    }
  }else{
    int counter = 0;
    while (counter <= 4) {
      if (nums[counter] == 9) {
        return true;
      }else{
       counter = counter + 1;
      } if (counter > 4) {
        return false;
      }
    }
  }
}

`

I understand I have to make sure that no matter what the code has to have some return value, but given the if and else statement, the length of the array is either less than 4 or greater than or equal to 4, so no matter what array is presented it should enter one of these conditionals?

njfy410
  • 139
  • 12
  • 7
    All of your returns are within if statements, and the compiler has determined that you've got potential logical pathways where nothing may be returned. Add a default return statement at the end of the method. – Hovercraft Full Of Eels Jul 25 '17 at 20:20
  • 1
    Java doesn't look at the conditions of while/if to see how many times they might compile. All it knows is, "a while loop may execute 0-N times," etc. – yshavit Jul 25 '17 at 20:22

2 Answers2

0

try this one

public boolean arrayFront9(int[] nums) {
        boolean result = false;
           if (nums.length < 4) {
            int counter = 0;
                while (counter != nums.length) {
                      if (nums[counter] == 9) {
                          result= true;
                        return result;
                      }else{
                        counter = counter + 1;
                      }
                } 

                if (counter > nums.length) {
                     result= false;
                        return result;
                    }
          } else{
                int counter = 0;
                    while (counter <= 4) {
                      if (nums[counter] == 9) {
                          result= true;
                            return result;
                          }
                      else{
                       counter = counter + 1;
                      } 
                    if (counter > 4) {
                        result= false;
                        return result;
                  }
                }
              }
        return result;
        }

actually you are not returning anything in the method but in your if/else.

Shaun
  • 25
  • 8
0

As a comment said, one of your code paths doesn't return a value.

This is much simpler.

public boolean containsNine()
{
    int maxCount = array.length < 4 ? array.length : 4;

    for (int i = 0; i < maxCount; i++)
    {
        if (array[i] == 9)
        {
            return true;
        }
    }

    return false;   
}
rianjs
  • 7,767
  • 5
  • 24
  • 40