View Single Post
  #1 (permalink)  
Old 07-23-2007, 12:13 AM
lenny lenny is offline
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
Recursion in java
Hello, here's my problem. I must take a string and through recursion display it backwards. Here's what I have thus far...

Code:
import TerminalIO.KeyboardReader; public class StringFun { public static void main(String [] args) { //Variables char runAgain='y'; String string; //Run Again feature while(runAgain=='y'||runAgain=='Y') { //Instantiate a keyboard reader KeyboardReader r=new KeyboardReader(); //User input string=r.readLine("Enter a string: "); System.out.println( "The string '"+string+"' displayed in reverse shows '"+string(string, string.length())+"'"); //Run again option runAgain=r.readChar("Run again (y/n)? "); } } //Recursive method to display the string in reverse static String string(String s, int pos) { if(pos>0) return s; else return string(s, pos-1); } }
Obviously I have something wrong because all I end up with is the string again. Any tips or hints on what I am doing wrong, or not doing?
Thanks.
Reply With Quote
Sponsored Links