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

Return problems

 
Kevin Magee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having problems with the following piece of code.

The complier is saying the method has no return statement, yet the same code worked in the first instance (at which point I had only the if statement in the for loop) Since I added the bracketing and extra statement I'm getting the error message.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apperetnly (and i may be wrong) the compiler thinks that there is a chance where the loop wont be executed even once.
in that case it need "reassurance" that there will be a return statement.
to solve this just add a simple return statement outside the loop like:
return "just for fun";
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are trying to return multiple things from one method, one from each iteration of your for loop, which is a no no. They way you have it the for loop will only execute ONE time, then one way or another hit a return which ends the method.
You can only have one thing returned, but it could be an array of Strings.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you are trying to do?
 
pam rowell
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well if i'm not mistaken i have had the same problem. you can only have one return statement i had a problem where i had multiple if's each with a return statement in brackets and it would always execute the first return the error would be something like statement not reached
 
Kevin Magee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.
 
Graeme Brown
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pam rowell:
well if i'm not mistaken i have had the same problem. you can only have one return statement i had a problem where i had multiple if's each with a return statement in brackets and it would always execute the first return the error would be something like statement not reached


pam
You are confusing two different issues, consider the following code

It is perfectly valid syntax to have more than one return statement. The method will terminate at the first one that executes. In this case only the first two can ever be reached, "return 3;" can never execute, the compiler checks for this.
Having said that, it is not considered good coding practise to have more than one exit point for a method. Cindy's code is an example of the right way to do it. I am simply posting this so that you may understand the compiler message the next time you see it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic