File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

missing return statement error from the FOR loop

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Ram
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc is perfectly correct, your confusion might be cleared now.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic