The Artima Developer Community
Sponsored Link

Java Answers Forum
Recursion Code I'm having issues with...

1 reply on 1 page. Most recent reply: Jun 15, 2005 11:05 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Luke

Posts: 5
Nickname: locoluke
Registered: Jun, 2005

Recursion Code I'm having issues with... Posted: Jun 15, 2005 5:18 PM
Reply to this message Reply
Advertisement
I am attempting to learn Java and am having a time of it. I am wanting to write a program that will allow a user to input number...then output them in reverse order. Is someone would be so kind as to look at it and provide me with suggestions or hints...or for those who are extremely kind hearted could tweak it for me and repost. On behalf of all those who recieve assistance here, "Thanks in advance!"

//*****************************************
// revstring - recursively reverse a string
// Name:
// Date:
//*****************************************
import java.io.*;


public class RevString
{

//******************************************************
// main method reads a string input by the user and
// calls recursive methods that print string in
//reverse
// and return reversed string
//******************************************************
public static void main(String[] args) throws
IOException
{
InputStreamReader isr = new InputStreamReader
(System.in);
BufferedReader stdin = new BufferedReader (isr);

System.out.println("Enter a String");
String input = stdin.readLine();
System.out.print("The input in reverse is --> ");

rev(input);
System.out.println();

// String strrev = retrev(input);
// System.out.println();
// System.out.println("The returned string is --> " +
// strrev);

}
// ****************************************************
// recursive method that prints the reverse of a string
// public static void rev (string r)
{
String strRemaining = "1";
String str = String.valueOf(r);
int iLen = str.length(); //get length for
indexing substrings

if (iLen <= 1)
System.out.println(str);

else
{
//save the x-1 characters of string
strRemaining = str.substring(0, (iLen - 1));
//put the last character into substring
str = str.substring((iLen - 1), iLen);
//print last character
System.out.print(str);
//revert string back to number
r = Integer.valueOf(strRemaining).intValue();
//call itself recursively
Reverse(r);
}
//*****************************************************
// recursive method that returns the reverse of a string
// public static String retrev(String r)
// {
// }
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Recursion Code I'm having issues with... Posted: Jun 15, 2005 11:05 PM
Reply to this message Reply
1. The line // public static void rev (string r) is commented. Why? This is you method definition

2. Where is the Method Reverse(String) ?




3. If you are really really lazy here's the solution:
public static String rev(String r) {
    if (r == null || r.length < 2) //this only works because Java won't look at the 2nd part if the first part results true
        return r; //to create a copy of r just write return new String(r)
    else
        return rev(r.substring(1)) + r.charAt(0);
}


Even more compact:
public static String rev(String r) {
    return (r == null || r.length < 2) ? r : (rev(r.substring(1)) + r.charAt(0));
}

Flat View: This topic has 1 reply on 1 page
Topic: Why iterator() method is required to explicitly synchronize. Previous Topic   Next Topic Topic: java for beginners????

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use