Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

JAVA Recursion Code Issue


  • Please log in to reply

#1
BigBadTex

BigBadTex

    New Member

  • Member
  • Pip
  • 3 posts
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. If 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, feel free to tweak it for me and repost. On behalf of all those who receive 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®;
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®;
}
//*****************************************************
// recursive method that returns the reverse of a string
// public static String retrev(String r)
// {
// }
}
}
  • 0

Advertisements


#2
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 875 posts
look here
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP