Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Missing return statement

 
justin villanueva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhh, beat me to it!
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Ohhh, beat me to it!


Who will dare to bit you sir?
 
justin villanueva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write method as



instead of

 
justin villanueva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pramod P Deore wrote:write method as



instead of


Now it says cannot find symbol and points to size
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you must have to declare variable size as

int size;
 
justin villanueva
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic