0

I have the following code

class dog{
    String name;
    int barksLeft;
    int bark(int howManyTimes){
        while(howManyTimes >3){
            System.out.println("woff!");
            howManyTimes-=1;
            return howManyTimes;
       }
     }
}

public class implementDog{
    public static void main (String[] args) {
        dog pupy = new dog();
        pupy.barksLeft = pupy.bark(6);
        System.out.println("You have "+pupy.barksLeft+" barks left");

    }
}

When I compile it, I get this error

implementDog.java:10: error: missing return statement
    }
    ^
1 error

Here's what I checked:

  • The "bark" method is not void and expects an int to be returned
  • The variable being return is of type int as well
  • What I want to do, is to populate the reference variable pupy.barksleft with the return value of the method, so that then I can print it.

What am I missing?

Pablo Gonzalez
  • 1,710
  • 3
  • 21
  • 36

4 Answers4

7

If you have a method returning something other than void, you need to make sure that every logical path through that method explicitly returns something of the appropriate object/type. In your case, consider the situation where howManyTimes is less than or equal to 3 - it will skip the while block, and go to the end. Since there is no explicit return statement there, the method will not return anything in that situation, which is incorrect - it should return an int in all cases.

It should also be noted that the last statement in a non-void method should either be a return or a throw.

radar
  • 595
  • 2
  • 11
  • 2
    +1 ; strangely, your answer is the only one that actually *answers* the question, pointing out to the fact that in non-void function/method, *every path has to return something*; if you'd just mention that in such cases *the last statement in non-void method always has to be either **return** or **throw** *, it'd be perfect. –  Jul 30 '14 at 19:31
  • Good call with the `throw`, I added that in :) – radar Jul 30 '14 at 19:35
2

You should put the return statement below the while() loop, after it closes. The way you've written the code, not only might it never return (e.g. if howManyTimes <= 3 it will not go through the loop), but the method automatically exits after one iteration of the loop even when howManyTimes > 3. return not only exits methods, it also causes the program to break out of any loops it is found in.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
1

Since while(howManyTimes >3) might never be true, you won't have any return statement in that case. You must add a return statement after the loop.

Eran
  • 387,369
  • 54
  • 702
  • 768
1

Try like this return statement below while

class dog{
        String name;
        int barksLeft;
        int bark(int howManyTimes){
            while(howManyTimes >3){
                System.out.println("woff!");
                howManyTimes-=1;


            }

           return howManyTimes;
         }

    }

    public class stackof{
        public static void main (String[] args) {
            dog pupy = new dog();
            pupy.barksLeft = pupy.bark(6);
            System.out.println("You have "+pupy.barksLeft+" barks left");

        }
    }
ImGeorge
  • 527
  • 6
  • 24
  • you should use the regular idiom `howManyTimes--;` if you're trying to provide an actual code as an answer, not to mention you should fix the formatting/indenting of the code too. –  Jul 30 '14 at 19:33