12 Replies Latest reply: Jan 28, 2009 4:35 PM by 807588

# for loop in a recursion

I am trying to write a program in which a series of numbers are generated by a recursive function based on strict number rules... such as the first number is always 94 and it can be only followed by 94,95,96,97, etc... I would like to get some help because I am doing a for loop in my recursive method, however the method just ends after one iteration and only display the first serie of numbers. I don't understand what I did wrong, so if someone could fix that part for me or tell me what is going wrong, I will appreciate a lot... Here is the code:

``````public class StringSeries {

static int nextInteger;
static int lastInteger;
static String numberWord = "";

public static int[] lastNumber(int k, int nextK){

if (k==94){

if (nextK==94){
int numEnd9494[]={97};
return numEnd9494;
}

if (nextK==95){
int numEnd9495[]={97,98};
return numEnd9495;
}

if (nextK==96){
int numEnd9496[]={97,98,99};
return numEnd9496;
}

if (nextK==97){
int numEnd9497[]={97,98,99};
return numEnd9497;
}

if (nextK==98){
int numEnd9498[]={98,99};
return numEnd9498;
}

if (nextK==99){
int numEnd9499[]={99};
return numEnd9499;
}

}

return null;
}

public static int[] nextNumber(int n){

if(n==94){
int numArray94[] = {94,95,96,97};
return numArray94;
}

if(n==95) {
int numArray95[] = {95,96,97,98};
return numArray95;
}

if(n==96) {
int numArray96[] = {96,97,98,99};
return numArray96;
}

if(n==97) {
int numArray97[] = {97,98,99};
return numArray97;
}

if(n==98){
int numArray98[] = {98,99};
return numArray98;
}

if(n==99){
int numArray99[] = {99};
return numArray99;
}

else
return null;
}

// When this method is called out for the first time, "nextInt" must be the same as "firstInt"
public static String generateSequence(int firstInt, int nextInt, int seqLength){

if (seqLength==1) {

for (int i=0; i<(lastNumber(firstInt,nextInt)).length; i++) {
lastInteger = (lastNumber(firstInt,nextInt));
numberWord = "" + lastInteger;
return numberWord;
}
}

else {

for (int j=0; j <(nextNumber(nextInt)).length; j++){
nextInteger = (nextNumber(nextInt))[j];
numberWord = nextInteger + " " + generateSequence(firstInt,nextInteger,seqLength-1);
return numberWord;
}
}

return null; //I don't understand why I have to put this line or else I get a compiler error

} // End of method

} // End of class

``````
• ###### 1. Re: for loop in a recursion
I don't know what's going wrong with your logic, but I'm not a fan of long-winded code. Consider:
``````static int[][] data = {
{94,95,96,97},
{95,96,97,98},
{96,97,98,99},
{97,98,99},
{98,99},
{99}
};

public static int[] nextNumber(int n){
return (94 <= n && n <= 99) ? data[n-94] : null;
}``````
• ###### 2. Re: for loop in a recursion
I understand what you are trying to say, i.e.:
``````public static int[] nextNumber(int n){
return (94 <= n && n <= 99) ? data[n-94] : null;
}``````
but this would have been practical if my problem was only about numbers from 94 to 99. In the actual problem that I have to solve, the numbers are not followed like 94,95,96,97,99.... I mean to say I have specific numbers such as 17,29, 37, 47, 53, 67, etc.... but in the sample I gave, I just wanted to make things easier so I put numbers from 94 to 99....

Anyway, I just wanted to know why my for loop is not getting executed properly in the recursion...
• ###### 3. Re: for loop in a recursion
In your method generateSequence you have return statements inside loops. That makes no sense:
``````for(int i =0; i<100; ++i) {
System.out.println(i);
return 42;
}``````
This code will only print the first value of i, right?
• ###### 4. Re: for loop in a recursion
I made that one change, but then I just get the last serie of numbers.... Is there like some small mistake with my for loop besides the one you had pointed out?
``````// When this method is called out for the first time, "nextInt" must be the same as "firstInt"
public static String generateSequence(int firstInt, int nextInt, int seqLength){

if (seqLength==1) {

for (int i=0; i<(lastNumber(firstInt,nextInt)).length; i++) {
lastInteger = (lastNumber(firstInt,nextInt));
numberWord = "" + lastInteger;
}
return numberWord;
}

else {

for (int j=0; j <(nextNumber(nextInt)).length; j++){
nextInteger = (nextNumber(nextInt))[j];
numberWord = nextInteger + " " + generateSequence(firstInt,nextInteger,seqLength-1);
}
return numberWord;
}

} // End of method                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ``````
• ###### 5. Re: for loop in a recursion
Is there a simple way to turn an iterative version into a recursive function?
I don't see any error in the for loop I wrote in the code but maybe things that are inside of the for loop are maybe not at the right place... I can't move ahead in solving my problem because I still don't understand what else is wrong in the recursive function? Can somebody please provide me some help..?? Thank you very much!
• ###### 6. Re: for loop in a recursion
Thank you!
In your method generateSequence you have return statements inside loops. That makes no sense:
``````for(int i =0; i<100; ++i) {
System.out.println(i);
return 42;
}``````
This code will only print the first value of i, right?
• ###### 7. Re: for loop in a recursion
ti_ma wrote:
but this would have been practical if my problem was only about numbers from 94 to 99. In the actual problem that I have to solve, the numbers are not followed like 94,95,96,97,99.... I mean to say I have specific numbers such as 17,29, 37, 47, 53, 67, etc.... but in the sample I gave, I just wanted to make things easier so I put numbers from 94 to 99....
So use a hash, same result.
• ###### 8. Re: for loop in a recursion
Use a hash for what? I need to use a recursive function to generate a series of numbers and that's what my thread is about but I ain't getting any answer...:(.... I don't care if my code is long when I put the numbers in separate arrays! I just want to know what is wrong in my recursive method...
jschell wrote:
ti_ma wrote:
but this would have been practical if my problem was only about numbers from 94 to 99. In the actual problem that I have to solve, the numbers are not followed like 94,95,96,97,99.... I mean to say I have specific numbers such as 17,29, 37, 47, 53, 67, etc.... but in the sample I gave, I just wanted to make things easier so I put numbers from 94 to 99....
So use a hash, same result.
• ###### 9. Re: for loop in a recursion
ti_ma wrote:
Use a hash for what?
Instead of those long code blocks, just as suggested with the arrays.
I need to use a recursive function to generate a series of numbers and that's what my thread is about but I ain't getting any answer...:(.... I don't care if my code is long when I put the numbers in separate arrays! I just want to know what is wrong in my recursive method...
You might not care if your code is long but you want US to look at it. And we do care.

Put some println() in and print everything. That should tell you.
• ###### 10. Re: for loop in a recursion
Oh well, I am sorry then... I did not know there was an easy way to put the arrays in few lines of code...
I did try to print everything and I only get some combinations of the numbers and not all of them....
jschell wrote:
ti_ma wrote:
Use a hash for what?
Instead of those long code blocks, just as suggested with the arrays.
I need to use a recursive function to generate a series of numbers and that's what my thread is about but I ain't getting any answer...:(.... I don't care if my code is long when I put the numbers in separate arrays! I just want to know what is wrong in my recursive method...
You might not care if your code is long but you want US to look at it. And we do care.

Put some println() in and print everything. That should tell you.
• ###### 11. Re: for loop in a recursion
ti_ma wrote:
Oh well, I am sorry then... I did not know there was an easy way to put the arrays in few lines of code...
I did try to print everything and I only get some combinations of the numbers and not all of them....
Yes, but that is in fact exactly the problem - you are not getting all of them.

So if you are printing out all of the values that are processed and then looking at the code it should be obvious why it is stopping too soon.

I suspect that perhaps you don't understand what I am talking about so in the method print out all of the following variables.

firstInt, nextInt, seqLength, i, j
• ###### 12. Re: for loop in a recursion
Well, I was just trying to print the sequence of length 4 but I should try to print the values of lastInteger and nextInteger, etc... Anyway, is there an easy way to turn an iterative version of a method into a recursive way? I first wrote the iterative version of the problem I have to solve and it is indeed generating all and exact combinations of how I am supposed to solve my problem in a recursive way....