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 error from the FOR loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "missing return statement error from the FOR loop" Watch "missing return statement error from the FOR loop" New topic

missing return statement error from the FOR loop

Rauhl Roy
Ranch Hand

Joined: Aug 01, 2006
Posts: 401

May i know what should i do if i want to return some thing from the loop in side a method.

please note: i want to print the string 2 times(twice)

[ March 08, 2008: Message edited by: Rauhl Roy ]
marc weber

Joined: Aug 31, 2004
Posts: 11343

The compiler is complaining because it's possible to call the method using an empty String ("") of length zero, in which case the loop body will never execute, so your method will be missing a return statement.

Keep in mind that when you "return" from a method, that method is done executing. So if you expect to iterate through the length of the String, you don't want to return during the first iteration.

I'm not exactly sure what you're trying to do here, but it looks like one approach might be to create a StringBuilder in the method, append it with each iteration of the loop, and then return a String representation of the StringBuilder after you've finished looping.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32
Marc is perfectly correct, your confusion might be cleared now.
I agree. Here's the link:
subject: missing return statement error from the FOR loop
It's not a secret anymore!