Simple Recursion

Newbie here, having a minor problem with some code. I need to use recursion to reverse a string, except when printed, only integers should be printed, all other non integer input should not. i.e. 7h3hk1k6 prints :6137. So I have a main method, a recursion method, and a method which tests if its an integer. I marked further down where i think the problem is, because it works fine if all input is either a single character or integer, a series of characters (string), or a series of integers. Not sure how to make mixed input work. Because i think once it finds a value thats not a character and returns false, thats it, doesn't look anymore. Thanks for any help.

public class Recursion {
public static void main (String args[]){
String strInput = "";
strInput =
JOptionPane.showInputDialog(null, "Please enter a string");
' + "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){ //here's the problem!!!

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;


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!