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.
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";
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.
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
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.