| | |
Recursion Program/Code I'm having issues with...
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Solved Threads: 0
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!" Code is as follows...and is attached (.java) as well.
//*****************************************
// 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)
// {
// }
}
}
//*****************************************
// 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)
// {
// }
}
}
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
You could always put it into a StringBuffer and reverse it:
Or you could use a recursive String reverse method:
Then you could go with highergrounds idea and use modulus:
Java Syntax (Toggle Plain Text)
import java.util.*; public class ReverseTest { public static void main(String[] args) { int reverseThisNumber = 12345678; StringBuffer sb = new StringBuffer(""+reverseThisNumber); System.out.println(sb.reverse().toString()); } }
Or you could use a recursive String reverse method:
Java Syntax (Toggle Plain Text)
public String reverse(String arg) { String tmp = null; if (arg.length() == 1) { return arg; } else { //extract the last char String lastChar = arg.substring(arg.length()-1,arg.length()); //extract the remaining chars String remainingString = arg.substring(0, arg.length() -1); tmp = lastChar + reverse(remainingString); System.out.println(tmp); return tmp; } }
Then you could go with highergrounds idea and use modulus:
Java Syntax (Toggle Plain Text)
public int revDigits (int num) { if (num < 10) { return num; } else { int s = num / 10; int d = num % 10; int i = revDigits(s); return (d * 10) + i; // incorrect // need to find out how many digits in d: //return (d * 10^(num digits in d)) + i); } }
![]() |
Similar Threads
- Code for Image Processing (C)
- trouble with knights tour (recursion) (C++)
- Sudoku Source Code (C++)
- need Recursion help (C)
- Thanks...Problem Solved--Re: Recursion Program/Code I'm having issues with... (Java)
- my first recursion program... :/ (C)
- !NEW!:::::!!Time Display Program!! (C++)
Other Threads in the Java Forum
- Previous Thread: Urgent Help Needed!!!! Plzzz
- Next Thread: Thanks...Problem Solved--Re: Recursion Program/Code I'm having issues with...
Views: 4716 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code color compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






