wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Return problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return problems" Watch "Return problems" New topic
Author

Return problems

Kevin Magee
Greenhorn

Joined: Feb 02, 2002
Posts: 18
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

Joined: Jan 13, 2002
Posts: 732
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Sep 29, 2000
Posts: 8521
Is this what you are trying to do?
pam rowell
Greenhorn

Joined: Jan 21, 2002
Posts: 7
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

Joined: Feb 02, 2002
Posts: 18
Thanks for the help.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return problems
 
Similar Threads
Problem Finding Index Value of a String in Array
Function Overloading issues
Appending to StringBuffer
where to use return statement
DataOutputStream object might not have been initialized????