Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-01-2008, 07:55 PM
Member
 
Join Date: Nov 2008
Posts: 13
Rep Power: 0
calicocal is on a distinguished road
Default Recusion Help
I am trying to use recursion to reverse a string. Here is what I have done so far. The problem is that java throws me an error saying that I am not returning a string. Any idea on what is happening?

public String reverse(String s){



if (start >= s.length()-1) {
String x=v.toString();
return x;
}


v.add(s.charAt(start));

start++;
reverse(s);
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-01-2008, 09:08 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
What variable is v? Put return anything after your reverse(s) call. It will either give you an unreachable code error or work how you intended it to.

The problem is if your condition doesn't fit the if statement then, regardless of having the recursive call, your program sees this as an incomplete method because nothing is returned.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-02-2008, 09:10 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,504
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Your method definition is this,

Code:
public String reverse(String s){
there is return a String. But where is the return statement in the method body?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-02-2008, 09:18 AM
serjant's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Ukraine,Zaporozhye
Posts: 486
Rep Power: 2
serjant is on a distinguished road
Send a message via ICQ to serjant Send a message via Skype™ to serjant
Default
I should confess,your imagination about recursion is so bad.I recommend to read some tutorials

Code:
public static String reverse (String s)
   {
     
     if (s.length() <= 1)
     {
       return s;                                     // stopping case
     }
     else
     {
       return reverse(s.substring(1)) + s.charAt(0); // recursion
     }
   }
I don't understand where you got some variables,your stopping case is really awful,and of course you have no any return statement,OMG those are the base things.

Last edited by serjant; 12-02-2008 at 09:21 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +2. The time now is 01:24 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org