# for loop in a recursion

**807588**Jan 28, 2009 9:37 PM

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

` `

- 15 Views
- Tags: none (add)