This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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?

 
Nicholas Censored
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nicholas Censored
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic