Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion Problem  RSS feed

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can reverse the order of a string using recursion. However, I do not know how to reverse the order of a string while only printing integers, i.e. 9h7fjf3h4 prints : 4379. This is what I have so far, but isn't working.

import javax.swing.JOptionPane;
public class A1Q2 {
public static void main (String args[]){
String strInput = "";
strInput =
JOptionPane.showInputDialog(null, "Please enter a string");
System.out.print(reverseDigits(strInput));
System.out.println('\n' + "End Processing...");
}

public static String reverseDigits(String strInput) {
String charOne,
message = "No digits were found";
char charTest;

// Base case
if (strInput.length() <= 1 && isCharAnInt(strInput.charAt(0)) == true){
return strInput;
}

//recursive call
charOne = strInput.substring(0,1);
charTest = charOne.charAt(0);

if(isCharAnInt(charTest) == true){
return reverseDigits(strInput.substring(1)) + charOne;
}
return message;
}

public static boolean isCharAnInt(char charTest){
boolean isCharAnInt = true;
if(charTest < '0' || charTest > '9'){
isCharAnInt = false;
}
return isCharAnInt;
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First, note that it would be the Java Way to use the static method java.lang.Character.isDigit(char) to test for digit-ness, rather than your own routine.

Second, if I were you, I would start from your working string-reversing program, and just add a condition to the existing return statement; "If the character is a digit, append the character; otherwise, don't. Return the result." Simple, yes?
 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, not familiar with the static method though, and i don't think my prof will mind too much, we don't cover it so I wouldn't be penalized for not using it. The program works fine, but every time I enter alot of characters, I get errors, when I enter a lot of integers, it's fine. Mixed input works fine unless to many characters are entered. Any thoughts?

import javax.swing.JOptionPane;

public class A1Q2 {

public static void main (String args[]){

String strInput = "";


strInput =
JOptionPane.showInputDialog(null, "Please enter a string");

System.out.print(reverseDigits(strInput));

System.out.println('\n' + "End Processing...");

}

public static String reverseDigits(String strInput) {

String charOne,
message = "Either nothing was entered or the single character was not an integer";

char charTest;

// Base case
if (strInput.length() <= 1 && isCharAnInt(strInput.charAt(0)) == true){

return strInput;
}
//recursive call
charOne = strInput.substring(0,1);
charTest = charOne.charAt(0);


if(isCharAnInt(charTest) == true){

return reverseDigits(strInput.substring(1)) + charOne;

}

return reverseDigits(strInput.substring(1));
}

public static boolean isCharAnInt(char charTest){

boolean isCharAnInt = true;

if(charTest < '0' || charTest > '9'){

isCharAnInt = false;

}

return isCharAnInt;

}
}
 
Anton Uwe
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a small problem with your Base case:

It misses the possibility that your input string ends with a letter.
So, the "Base case" must be coded as follows:

 
John Lockheart
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, program works. I appreciate the help.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!