Hans Hovan

Ranch Hand

Posts: 35

posted 4 years ago

I'm using a main method which calls another method (public static int countDigits(int number)) that is supposed to calculate the number of digits in an integer recursively. The user inputs the number in the main method and the countDigits method is called there.

I've been able to figure out the logic behind the problem, but am having some trouble implementing it.

I wrote it out on paper and figured that I need to divide the number by 10 and if the answer is anything other than 0 to store that fact in a variable (sum = 1 for example). Then, divide the resulting number by 10 again and if the sum is still not 0 then +1 to sum. Continue this process until the number/10 = 0 (my base case, which is a single digit number) and then return whatever the sum added up to be.

I'm still trying to learn how recursions work. So far I coded(below). If I'm understand recursions correctly though when I am having the method call itself it starts from the beginning and resets sum (which is what I am using to keep track of how many time 10 can divide into the number before it equals 0) to int sum = 1. However, I'm not quite sure how else to approach this? Any hints/solutions/explanations would be appreciated. Thanks for the help.

I've been able to figure out the logic behind the problem, but am having some trouble implementing it.

I wrote it out on paper and figured that I need to divide the number by 10 and if the answer is anything other than 0 to store that fact in a variable (sum = 1 for example). Then, divide the resulting number by 10 again and if the sum is still not 0 then +1 to sum. Continue this process until the number/10 = 0 (my base case, which is a single digit number) and then return whatever the sum added up to be.

I'm still trying to learn how recursions work. So far I coded(below). If I'm understand recursions correctly though when I am having the method call itself it starts from the beginning and resets sum (which is what I am using to keep track of how many time 10 can divide into the number before it equals 0) to int sum = 1. However, I'm not quite sure how else to approach this? Any hints/solutions/explanations would be appreciated. Thanks for the help.

posted 4 years ago

Hi, Hans, and welcome to the Ranch!

Your method is equivalent to this:

Part of recursion is that each pass uses the result of the pass that it calls, but you're not doing that at all.

For example, here's a recurseve definition of factorial:

factorial(N):

if N = 0, then 1

else N times factorial(N-1)

First write a similar English or pseudocode definition for your algorithm. Then translate that definition to Java, and make sure you can see how each piece translates.

Your method is equivalent to this:

Part of recursion is that each pass uses the result of the pass that it calls, but you're not doing that at all.

For example, here's a recurseve definition of factorial:

factorial(N):

if N = 0, then 1

else N times factorial(N-1)

First write a similar English or pseudocode definition for your algorithm. Then translate that definition to Java, and make sure you can see how each piece translates.

It is sorta covered in the JavaRanch Style Guide. |