File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "missing return statement?" Watch "missing return statement?" New topic

missing return statement?

Nicholas Censored

Joined: May 21, 2005
Posts: 2
Hi there.
I got a problem with some of my code, but I just can't see what the problem is. Can you help me out on this one?

LasIn.lasHeltal() reads int-inputs from the console, there's no problem with that one atleast.

Now this is what I get when the compiler gets to this part:

Any help appreciated :roll:
(I'm from Sweden)
[ May 21, 2005: Message edited by: Nicholas Censored ]
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
gomorron Nicke! hur �r v�dret dit p� r�tta sidan av Atlanten?

i think i see your problem - the only return statements in your method are wrapped up in an if-else block, and the compiler isn't smart enough to understand that either the "if" or the "else" will necessarily be executed. it thinks there might be some way for control to pass by the whole "if" statement entirely, in which case it would fall off the end of the method without ever "return"ing anything.

the easy way to fix this is to put in a bogus, unreachable return statement after the end of the "else" block, just before the closing brace of the method. this should make the compiler happy.

on edit: there is also a hard way to fix your problem, which would involve thinking about the structure of your code. look at the "while" loop, specifically, and ponder whether or not it is really needed. how many times will it run, and why?
[ May 21, 2005: Message edited by: M Beck ]
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
imagine that boolean stop allways evaluates true; in that case the loop never executes, and so never outputs a return statement...

so,after your while end bracket write:

return myInt;

hope this helps

java amateur
Nicholas Censored

Joined: May 21, 2005
Posts: 2
V�dret �r mulet liksom �lgens n�sa (mulen).

I solved my problem by rewriting the calling code and then removed the while-loop.

Thanks for your help!

I agree. Here's the link:
subject: missing return statement?
It's not a secret anymore!