# recursive method

Ranch Hand
Posts: 64
I am working on a recursive method that accepts an int and reduces it to a single digit. It is part of my numerology program. I keep getting an error from the compiler when I call the recursive method. I think maybe I am calling it wrong, but I don't know the right way. We didn't go over it in class... My source code is below:

import java.text.NumberFormat;
public class Calculation {
public static void main(String[] args) {
String s = ("04/04/1967");
int newmonth = Integer.parseInt(s.substring(0,2));
int newday = Integer.parseInt(s.substring(3,5));
int newyear = Integer.parseInt(s.substring(6,10));
int num = (newmonth + newday + newyear);
System.out.println(num);
System.out.println(reduce_num(num));
}
static int reduce_num(int num) {
String birthdate = (String.valueOf(num));
System.out.println(birthdate);
int num1 = Integer.parseInt(birthdate.substring(0,1));
int num2 = Integer.parseInt(birthdate.substring(1,2));
int num3 = Integer.parseInt(birthdate.substring(2,3));
int num4 = Integer.parseInt(birthdate.substring(3,4));
num = (num1 + num2 + num3 + num4);
if (num <= 9) <br /> return num;<br /> else <br /> if (num >= 10)
reduce_num;
return num;
}
}

Jag Sharma
Greenhorn
Posts: 10
When you call reduce_num recursively in reduce_num the function expects a paramater. I am not sure, if the program will yeild the desired results, because your subscripts are always fixed 0-4, where as the length of the number will decrease nd you may get out-of-bounds exception. What are you trying to do exactly?

Ranch Hand
Posts: 64
Yes, I was wondering if it would work, too, because of the number being fixed.
I am doing a numerology program. This method is supposed to accept an int, and then reduce it to a single digit.
The Calculation class takes the String of the person's birthdate and then divides it into individual Strings, which are then parsed and converted to ints and added together, and then that number is passed to the reduce_num method, which converts it to a single digit integer.

Michael Bruesch
Ranch Hand
Posts: 158
OK, first of all, when you call reduce_num recursively inside of itself, you don't send it any parameters, but you defined that method to take an int as a parameter. I love these school assignments, so I came up with a program of my own that finds this "cosmic" number that you are looking for:

Please do not simply copy my code and turn it in, only use it for a reference. Else you'll never learn. Good luck!
PS: sorry about the variable in the getDigit function, it was one of my C++ functions and forgot to change int_result to standard Java format of intResult.

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
[This message has been edited by Michael Bruesch (edited November 11, 2001).]
[This message has been edited by Michael Bruesch (edited November 11, 2001).]

Ranch Hand
Posts: 64
Thank you.

Junilu Lacar
Bartender
Posts: 7750
61
I maintain my position about assignments and will give you this hint:
reduce_num() can be reduced to a single if-else statement: