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
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;
}
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....for(int i =0; i<100; ++i) {
System.out.println(i);
return 42;
}
This code will only print the first value of i, right? // 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
BigDaddyLoveHandles wrote:
In your method generateSequence you have return statements inside loops. That makes no sense:
This code will only print the first value of i, right?for(int i =0; i<100; ++i) { System.out.println(i); return 42; }
ti_ma wrote:So use a hash, same result.
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....
jschell wrote:ti_ma wrote:So use a hash, same result.
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....
ti_ma wrote:Instead of those long code blocks, just as suggested with the arrays.
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...You might not care if your code is long but you want US to look at it. And we do care.
jschell wrote:ti_ma wrote:Instead of those long code blocks, just as suggested with the arrays.
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...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.
ti_ma wrote:Yes, but that is in fact exactly the problem - you are not getting all of them.
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....