-2

My method is for example, supposed to output 123 if int[] num123 = {1,2,3}; in the main method. It is outputting zero instead. In the convert num method when I change that last zero it just outputs whatever number it was replaced with. We are not allowed to use any loops so that is what has me stumped.

 public int convertNum(int[] num) {

    return numHelper(num, 0, num.length - 1, 0);

}

private int numHelper(int[] num, int atIndex, int lastIndex, int result) {

    atIndex = num.length - 1;
    if (atIndex == lastIndex) {
        return result;
    }

    if (num.length > 0) {
        atIndex += 1;

    }

    return (int) (num[atIndex] * Math.pow(10, lastIndex - atIndex))
            + numHelper(num, atIndex + 1, lastIndex, result);

}
J.Doe
  • 31
  • 2
  • 8
  • 4
    What is the point of passing in parameter `atIndex`, when the first thing you do is replace it with `atIndex = num.length - 1`, **instantly ending the recursion**? – Andreas Apr 17 '17 at 20:45
  • fixed that. now it is outtputting 20 – J.Doe Apr 17 '17 at 20:46
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Ousmane D. Apr 17 '17 at 20:48
  • 2
    You're making `numHelper` an awful lot harder than it needs to be by using all that casting and pow malarky. Just pass `10 * result + num[atIndex]` as the `result` parameter. – Andy Turner Apr 17 '17 at 20:49
  • Ok, now if input has length 1, `atIndex` (0) is equal to `lastIndex` (0), so you return `result` (0). See a problem there? --- Also, what is the purpose of `if (num.length > 0) { atIndex += 1; }`? It likely has no purpose. --- In short, check your logic, by manually stepping through the code and write on paper what is happening. Or use a debugger: [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Apr 17 '17 at 20:50
  • 1
    My code is should be converting an array to just an int. So int [] num= {1,2,3} should become 123. But for some reason it is outputting incorrectly. – J.Doe Apr 17 '17 at 20:50
  • Step through it in the debugger built into your IDE, inspect the values of your variables at least stage in the process, and it will become clear. As @AndyTurner says, no need to use `Math.pow` (although that is certainly one valid approach), and no need to pass the result in as an argument (just use the return value). Also note that it's much simpler if you start at `num.length - 1` and *decrement* `atIndex` each time, having `numHelper` return `0` if `atIndex < 0` and otherwise multiplying the result of the recursion by 10 and adding in `num[atIndex]`. – T.J. Crowder Apr 17 '17 at 20:52

1 Answers1

0

You might change some logic in your recursion. You can pass just the nums[], index, and result.

private int numHelper(int[] nums, int atIndex, int result) {
    if (atIndex == nums.length) {
        return result;
    }

    int check = result;                   // save previous result
    result = result * 10 + nums[atIndex];

    // result might cycle through Integer.MIN_VALUE after hitting the Integer.MAX_VALUE
    if(check > result) {
        throw new NumberFormatException();
    }
    return numHelper(nums, ++atIndex, result);
}

Call this method from your main class or any other method as

public int convertNum(int[] num) {
    // say, nums = new int[]{1,2,3}
    return numHelper(num, 0, 0);    // atIndex and result are 0
}

Sample test
Input: nums = { 1,2,3} Output: 123
Input: nums = { 8,9,1,2,3,4,9,3,5,1 } Output: java.lang.NumberFormatException
Input: nums = { 8,9,1,2,3,4,9,3,5,1 } Output: 322414759 when you comment the throw keyword

Notice how you might get incorrect result due to Integer looping through the range.
Note: Make sure that your array has elements within Integer.MAX_VALUE | 2147483647 range and make use of throw new NumberFormatException(); always.

Devendra Lattu
  • 2,732
  • 2
  • 18
  • 27