jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes recursive method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursive method" Watch "recursive method" New topic

recursive method

Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
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);
static int reduce_num(int num) {
String birthdate = (String.valueOf(num));
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)
return num;
Jag Sharma

Joined: Nov 09, 2001
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?
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
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

Joined: Sep 23, 2001
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).]

Michael J Bruesch<br /><i>I code, therefore I am.</i>
Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
Thank you.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

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

Additional hints:
- % can give you the digit in the 1s place
- integer division results in an integer (remainder is ignored)
- who said you can't call reduce_num() more than once?
Good luck!

[This message has been edited by JUNILU LACAR (edited November 12, 2001).]

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link: http://aspose.com/file-tools
subject: recursive method
It's not a secret anymore!