my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Missing return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Missing return statement" Watch "Missing return statement" New topic
Author

Missing return statement

justin villanueva
Greenhorn

Joined: Feb 02, 2011
Posts: 9
Hiya, I have some code that I'm just messing around with while reading Head First java. So this really isn't serious code.

Now my problem is that I keep getting the error "Missing return statement" and it points to a }

So heres my code



Pretty much it asks for the size of a kitty and then it runs the if statement.

Any help would be much appreciated
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

The problem is here


you must have to write return type as void instead of int. Because you are not returning anything from this method.


Life is easy because we write the source code.....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

The compiler doesn't like to be lied to. In this method:
you tell the compiler that you will be returning an int, but then don't. This ticks it off.

So either return an int, or tell the compiler that you don't intend to return anything (which is probably what's best) by declaring the return type as void.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Ohhh, beat me to it!
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Bear Bibeault wrote:Ohhh, beat me to it!


Who will dare to bit you sir?
justin villanueva
Greenhorn

Joined: Feb 02, 2011
Posts: 9
Bear Bibeault wrote:The compiler doesn't like to be lied to. In this method:
you tell the compiler that you will be returning an int, but then don't. This ticks it off.

So either return an int, or tell the compiler that you don't intend to return anything (which is probably what's best) by declaring the return type as void.

How do I declare the return type as void?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

write method as



instead of

justin villanueva
Greenhorn

Joined: Feb 02, 2011
Posts: 9
Pramod P Deore wrote:write method as



instead of


Now it says cannot find symbol and points to size
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Then you must have to declare variable size as

int size;
justin villanueva
Greenhorn

Joined: Feb 02, 2011
Posts: 9
Pramod P Deore wrote:Then you must have to declare variable size as

int size;

D'oh! I knew that, had the wrong variable! Its working now Thanks all!

Now, maybe one more question. Why didn't it return an int in the first place?

In the example from the book, instead of receiving a size for the kitty from the user, the size is added into the code (one.setSize(70); for example)
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

When declaring any method like



here you are telling that this method returning int (myInt). And when you are not returning any value from method then you have to write return type as void. In your method



you are not returning any value therefore you had write void here instead of int.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Missing return statement