-4
public class array
{
   public static void main (String[]args)
    {
    
    }
    public static int[] create (int size)
    {
    int[]a=new int [size];
    for (int i=2;i<a.length;i=i+2)
    {
    a[i]=i;
    }
    return a;
    }
    public static void print(int[]a)
    {
    for (int i=2; i<a.length;i++)
        {
        System.out.print(a[i]);
        }
    
    }
    
    public static boolean found(int[]a,int item)
    {
    for (int i=2;i<a.length;i++)
    {
    
    if(a[i]==item)
    {
    return true;
    }
    else
    {
     return false;
    }
    
    
    
  }
 }

    public static int[] grow(int[]a)
    {
    int[]b=new int[a.length+1];
    b[0]=0;
    for (int i=0; i<a.length;i++)
    {
    b[i]=a[i-1];
    }
    return b;
    }

}

This is my array coad.

  1. size 2.print 3.found the value false or true 4.add number the front part Did I do something wrong? What means a missing return statement? when I try to compile this it doesn't work. Is there something wrong with this? the error is in the boolean last bracket. But I can't find the wrong part. It seems all correct.
Hello
  • 1
  • 1
  • 1
    Your code is expecting to have a value returned. Somewhere in your code there is no guaranteed value to be returned. – Dale Jan 14 '21 at 18:26
  • 2
    Hint: Consistently indenting and formatting your code to be human-readable will make it easier for you, as a human, to read the code and spot logical problems like this. – David Jan 14 '21 at 18:28
  • 1
    In the `found` function, what gets returned when the `for` loop is never entered? – 001 Jan 14 '21 at 18:28
  • 1
    Does this answer your question? ["Missing return statement" within if / for / while](https://stackoverflow.com/questions/23058029/missing-return-statement-within-if-for-while) – Spectric Jan 14 '21 at 18:42

4 Answers4

1

If a method has a return type, then you must return a value. Your method public static boolean found(int[] a, int item) has a return type bool and so must return a true or false. Inside the method, you are iterating over an array, but if that array is empty you'll never go inside the block and so will never return true or false. This is why the compiler is complaining. (See comment below)

public class HaileyKim_array {
  //omitted code

    public static boolean found(int[] a, int item) {
        for (int i = 2; i < a.length; i++) {

            if (a[i] == item) {
                return true;
            } else {
                return false;
            }
        }
        
        return false; // <<<<<< here you need to return something because if the array a[] is empty, it'll jump over the if statement
    }

 // omitted code
}
MSpeed
  • 8,153
  • 7
  • 49
  • 61
  • There is a comment in the code that explains what has changed. But I will write a sentence above also to explain – MSpeed Jan 14 '21 at 18:31
  • Note that in this logic the `for` loop in the `found` method isn't needed at all. The method only ever checks the value of one element in the array. – David Jan 14 '21 at 18:32
  • MSpeed. You are right. The comment is good. I did not read carefully enough. But just in case there are more lazy readers like me, you might benefit from a prose sentence. Nevertheless, strictly speaking this was my mistake. ;-) – Yunnosch Jan 14 '21 at 18:33
1

If a method has a return type, it must return something in any possible case. The compiler needs to be able to verify that.

for (int i=2;i<a.length;i++)
{
    if(a[i]==item)
    {
        return true;
    } else
    {
        return false;
    }
}

The loop would iterate over all items but it returns after the first item.

If there is no item however, it doesn't return anything.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
dan1st
  • 12,568
  • 8
  • 34
  • 67
  • _The loop **would** iterate over all items **but it returns after the first item.**_ - as I said – dan1st Jan 14 '21 at 18:32
  • Oh dear. I should get some sleep. My mistake. Second in a few minutes and on the same page..... – Yunnosch Jan 14 '21 at 18:34
  • 1
    Errare humanum est. – dan1st Jan 14 '21 at 18:36
  • To repay, I did some pretty-making, hope you like it. I also propose to change "is no item" to "are less than 3 items", because I think for 0, 1 and 2 items the loop is never entered. – Yunnosch Jan 14 '21 at 18:42
0

You have missed return statement here:

public static boolean found(int[] a, int item) {
    for (int i = 2; i < a.length; i++) {
        if (a[i] == item) {
            return true;
        } else {
            return false;
        }
    }
}

Make it like this:

public static boolean found(int[] a, int item) {
    for (int i = 2; i < a.length; i++) {
        if (a[i] == item) {
            return true;
        } else {
            return false;
        }
    }
    // return something based on your business logic
    return true;
}

This happened because compile is smart enough to understand that this code may not return any value because this condition:

        if (a[i] == item) {
            return true;
        } else {
            return false;
        }

may never happened. So your code will run till the end of method found and at the end there is no return.

Alex Crazy
  • 232
  • 1
  • 2
  • 12
  • The loop in 'found' is pointless, since it executes either no times at all if a.length <= 2, or exactly once if a.length > 2. – guest Jan 14 '21 at 18:35
  • This is another problem, but the question was about missing return statement – Alex Crazy Jan 14 '21 at 18:39
  • Perhaps, but there seems little to gain by waiting for that question to be posted, when the OP finds the code is still not working after fixing the 'return'. – guest Jan 25 '21 at 21:59
0

shortly, in the function: found(), you did not relate to the case that the array has length 0 or 1 or it is a null pointer. so add "return false;" statement at the end of the function.

Ventura
  • 83
  • 1
  • 9