简体   繁体   中英

Having Trouble With a Recursion Problem that Converts a String to an Int

So the problem is as follows. You are to create a method that takes a string as a parameter that consists of all numbers and you need to take that string and convert it to an Integer. You have to use a recursive solution to solve the problem.

I have written the following code and it works just fine when I enter "1234" or "138775" but as soon as I enter a number that contains 0 it returns a weird result.

100, 1001, 101, and 12045 return 10, 11, 110, and 1245 respectively. As mentioned above The code works just fine when I send it things like "1234" or "14384" but as soon as there is a zero it has a tendency to remove that zero.

I have tried different int to string conversions from the Integer class such as parse(int) but that had the same result.

/**
 * converts a string of numbers to an int
 * 
 * @param String str: original string of numbers
 * @return int recursiveStringInt: returns the int value of the string
 */
public static int recursiveStringInt(String str)
{
    if(str.length() == 1)
        return Integer.valueOf(str);
    else
    {
        return Integer.valueOf(str.substring(0,1) + recursiveStringInt(str.substring(1)));
    }
}

Thanks for your guys help!

Please let me know if there is any clarification needed.

Your parentheses are a bit off for what you're trying to do, and the logic needs some work.. what you want is to take the last character of the current string, convert it to an integer, and add it to the conversion of the front of the string times 10.

int recursiveStringInt(String str) {
    int length = str.length()
    if(length == 1)
        return Integer.valueOf(str);
    else
    {
        int temp = Integer.valueOf(str.substring(length-1)) + ( 10 * recursiveStringInt(str.substring(0,length-1)));
        return temp;
    }
}

Trivial case of "8" results in just the first block being executed.

Next case of "83" results in temp = 3 + (10 * 8) = 83

Next case of "103" results in temp = 3 + (10 * (0 + (10 * 1))) = 103

Try using a divide and power solution

public static void main(String[] args) {

    System.out.println(recursiveStringInt("12034", 0));

}

public static int recursiveStringInt(String str, int pow)
{
    if(str.length() < 1)
        return 0;
    else
    {
        int temp = Integer.valueOf(str.substring(str.length() -1)) 
                                                       * (int) Math.pow(10.0, 1.0 * pow);
        temp += recursiveStringInt(str.substring(0, str.length() -1), pow + 1);
        return temp;
    }
}

This is because when you parse a substring like 054 to int, it becomes 54 .

Try this code:-

public static int recursiveStringInt(String str) {
    return str.length() == 1
            ? Integer.valueOf(str)
            : (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
}

I have used this logic:-

105 = 1*100 + 0*10 + 5*1

Edit: If you don't understand the ternary operator, here is the if-else version:-

public static int recursiveStringInt(String str) {
    if (str.length() == 1) {
        return Integer.valueOf(str);
    } else {
        return (int) (Integer.parseInt(str.substring(0, 1)) * Math.pow(10, str.length() - 1)) + recursiveStringInt(str.substring(1));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM