944,042 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5428
  • Java RSS
Jun 15th, 2005
0

Recursion Program/Code I'm having issues with...

Expand Post »
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)
// {
// }
}
}
Attached Files
File Type: java RevString.java (1.9 KB, 9 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CoolHandLuke is offline Offline
4 posts
since Jun 2005
Jun 15th, 2005
0

Re: Recursion Program/Code I'm having issues with...

the easiest way to break a number down right to left is to use the mod function
rightDigit = wholeNumber % 10 will give you the right most digit then all you have to do is subtract the rightDigit from the wholeNumber, divide it by 10 and pass the wholeNumber back into the parameter
Reputation Points: 10
Solved Threads: 1
Newbie Poster
higherGround574 is offline Offline
21 posts
since Feb 2005
Jun 16th, 2005
0

Re: Recursion Program/Code I'm having issues with...

You could always put it into a StringBuffer and reverse it:
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class ReverseTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. int reverseThisNumber = 12345678;
  8. StringBuffer sb = new StringBuffer(""+reverseThisNumber);
  9. System.out.println(sb.reverse().toString());
  10. }
  11. }

Or you could use a recursive String reverse method:
Java Syntax (Toggle Plain Text)
  1. public String reverse(String arg)
  2. {
  3.  
  4.  
  5.  
  6. String tmp = null;
  7. if (arg.length() == 1)
  8. {
  9. return arg;
  10. }
  11.  
  12. else
  13. {
  14.  
  15. //extract the last char
  16. String lastChar = arg.substring(arg.length()-1,arg.length());
  17.  
  18.  
  19. //extract the remaining chars
  20. String remainingString = arg.substring(0, arg.length() -1);
  21.  
  22. tmp = lastChar + reverse(remainingString);
  23. System.out.println(tmp);
  24. return tmp;
  25.  
  26. }
  27.  
  28. }

Then you could go with highergrounds idea and use modulus:
Java Syntax (Toggle Plain Text)
  1. public int revDigits (int num) {
  2. if (num < 10) {
  3. return num;
  4. } else {
  5. int s = num / 10;
  6. int d = num % 10;
  7.  
  8. int i = revDigits(s);
  9.  
  10. return (d * 10) + i; // incorrect
  11.  
  12. // need to find out how many digits in d:
  13. //return (d * 10^(num digits in d)) + i);
  14. }
  15. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Urgent Help Needed!!!! Plzzz
Next Thread in Java Forum Timeline: Thanks...Problem Solved--Re: Recursion Program/Code I'm having issues with...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC