-2

This is my block of code

public static boolean noTriples(int[] nums) { 
        for (int i = 0; i < nums.length; i++) {
            return (!(nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]));
        }
    }


public static void main(String[] args) {
        System.out.println(noTriples(new int[]{1, 1, 2, 3, 1}));
        System.out.println(noTriples(new int[]{1, 1, 2, 2, 2}));
        System.out.println(noTriples(new int[]{1, 1, 1, 2, 2, 2, 1}));
    }
}

It says there is a missing return statement. This is undoubtedly because my return statement is in my for loop, but I'm a java noob and I've been messing around with it on the outside and I keep making new problems for myself.

jf4i2d
  • 27
  • 1
  • 8
  • 11
    what happens if nums.length == 0? – giorashc Jan 15 '20 at 19:44
  • Even if *you* are sure you will return - the compiler doesn't care. You need a base case - say `return false;` outside and after your for loop. – sleepToken Jan 15 '20 at 19:45
  • 8
    Also, you are returing from the function at the very first iteration, what's the point of having a loop ? – Nicolas Jan 15 '20 at 19:45
  • 1
    You probably want an `if` for that `return` instead, and `return true` inside of the `if` so that it won't just return every time on the first iteration. – Nexevis Jan 15 '20 at 19:47

4 Answers4

1

Your return is inside the for loop, but it might not execute at all if the array is empty (thus nums.length is 0), hence the compiler is complains a return is needed.

You can initialize the return value to a default (e.g false) and update it in the loop. Something like this

boolean isSomething(int[]x) {
   boolean result = false;
   for ( .... ) { 
        if (someConditionIsMet) {
          result = true;
           break;
         }
    }
   return result
}
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

There is a two things wrong with your code

  • Even if you know the nums array is never going to be empty, you still need to return something if it is, that's your main problem.
  • You are returning from your function, not from the loop, at the very first iteration. Even though your array might have multiple elements, you are mostly working with the first three.

Here is a revised version of your code

public static boolean noTriples(int[] nums) {
    for (int i = 0; i < nums.length; i++) {
        // If the condition is true for the current element, we return. 
        // If not, we continue to the next element
        if (!(nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2])) {
            return true;
        }
    }
    // in case our array is empty, we return false.
    // and we never go through our loop
    // or maybe, no element matches our condition.
    return false;
}

Some people prefer not having multiple return statement in a function. In this case, you could instanciate a boolean variable, that would be false at start, and rather than returning true in your loop, you could change the value of this variable. You would then return the variable.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
1

This is because compiler is not sure if you will actually return it inside the for loop. Middle part of the for loop is a condition so in case if it's false from the start, loop will not be executed and value will not be returned. Same would happen if you try to return inside an if statement without an else.

        boolean flag=false;
        for(int i=0;flag;i++){
            System.out.println("This will not be printed");//this will not be printed
        }

If you do something like this:

        for(int i=0;true;i++){
            return "value";
        }

Compiler is sure that you return a value and therefore it won't complain.

wisnix
  • 180
  • 1
  • 8
0

You must return outside of the loop. You are getting compile time error because you are returning inside the loop. You must compute boolean value true or false inside the loop and then return outside of the for loop.

Gaurav Dhiman
  • 953
  • 6
  • 11
  • You don't need to compute it inside and `return` on the outside, you can `return` it on the inside just fine, you just need to handle the other situations accordingly. You seem to imply he cannot do `return true` from within the loop. – Nexevis Jan 15 '20 at 19:55
  • he can but whats the use of loop if returning at first iteration? – Gaurav Dhiman Jan 15 '20 at 19:56
  • That is a different error with his code, he most likely wants to do an `if` with that condition, and `return true` when that condition is `true` so it won't return immediately every time, instead the `false` conditions will continue to iterate the loop until the first `true` is found. Inside the `if` he can still write `return true;`. – Nexevis Jan 15 '20 at 19:57