0

I'm attempting to generate a random four digit number with no repeating digits. I have a method to generate the number and two to check for length and repetition. It compiles and works as expected, however, occasionally, I will get a StackOverflowError while it is running. Here is the block of code where it seems to be having a problem with:

//ensures that generated pattern is four digits long
public void randomCheck(){
    int patternNum = Integer.parseInt(pattern);
    if(patternNum<1000){
        numGen();
    }
    else{
        repeatCheck();
    }
}
//ensures that pattern is unique
public void repeatCheck(){
    solutionNumber();
    if((secondSolnDigit==firstSolnDigit)||(firstSolnDigit==thirdSolnDigit)||
    (firstSolnDigit==fourthSolnDigit)||(secondSolnDigit==thirdSolnDigit)||
    (secondSolnDigit==fourthSolnDigit)||(thirdSolnDigit==fourthSolnDigit)){
        numGen();
    }
    else{
    return pattern;
    }
}
//generates random number
public void numGen();{
    Random rand = new Random();
      int randomNum = rand.nextInt(10000);
      String patternString = Integer.toString(randomNum);
    pattern = patternString;
    randomCheck();
}
Smo
  • 41
  • 1
  • 6
  • Your methods calls one an other and it not able to complete execution so the heap keep growing till it runs out of memory ! – StackFlowed Oct 22 '14 at 15:02
  • 2
    I ask myself how hard can be searching [`StackOverflowError`](http://docs.oracle.com/javase/8/docs/api/java/lang/StackOverflowError.html) and read its description: *Thrown when a stack overflow occurs because an application recurses too deeply* – Luiggi Mendoza Oct 22 '14 at 15:03
  • How can I modify my algorithm so that this doesn't happen? – Smo Oct 22 '14 at 15:04
  • Out of curiousity, why not put the numbers into a list, shuffle it, then pop off 4? That way, you're guaranteed uniqueness and four numbers. – Chris Forrence Oct 22 '14 at 15:04
  • @ChrisForrence That sounds like a good solution but I don't know how to go about doing it. Still pretty new to Java – Smo Oct 22 '14 at 15:07
  • 1
    possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) – ToYonos Oct 22 '14 at 15:12

2 Answers2

0

StackOverflowError is when you have to many method calls stacked on one another. There is a limit in Java to how many method calls can be stacked on one another.

You are calling randomCheck() from your numGen() method and calling numGen() from your randomCheck() method this appears to be the problem. Your code is probably chasing it's own tail. If numGen() is not called from randomCheck() then repeatCheck() is which also calls numGen().

It may work fine sometimes if it reaches the end of method calls before the stack runs out of space. But in some cases it is making more method calls than others adding more calls to the stack and eventually running out of space. That is why it works sometimes and not others.

You could try using a loop to accomplish the same thing. This way you won't have to worry about StackOverflowError.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

The stack overflow error happen when a there is too many method call in each other. It generally happen in recursive methods. Here it happens because you call numGen() from randomCheck() and repeatCheck() then numGen() call back randomCheck().

The solution here is to test in a while loop, remove you method randomCheck() and replace your code by:

//ensures that pattern is unique AND four digits long
public void repeatCheck(){
    solutionNumber();
    int patternNum = Integer.parseInt(pattern);
    while((secondSolnDigit==firstSolnDigit)||(firstSolnDigit==thirdSolnDigit)||
    (firstSolnDigit==fourthSolnDigit)||(secondSolnDigit==thirdSolnDigit)||
    (secondSolnDigit==fourthSolnDigit)||(thirdSolnDigit==fourthSolnDigit) || patternNum<1000){
        numGen();
    }

    return pattern;
}
//generates random number
public void numGen();{
    Random rand = new Random();
    int randomNum = rand.nextInt(10000);
    String patternString = Integer.toString(randomNum);
    pattern = patternString;
}

Moreover why do you cast your integer in string and then cast it back in integer, can't you just use the int as it is generated? and convert it after if you really need it as a string.

Hope this helped.

Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63
  • Hmm. I've tried this but for some reason I'm still getting a runtime error for my nonexistent randomCheck(). Is there a cache or something that I should delete? I recompile it every time I run it – Smo Oct 22 '14 at 16:30
  • What do you mean ? You don't have compilation errors for the non existent `randomCheck()` ? – Ludovic Feltz Oct 22 '14 at 16:33
  • Ok, so I just deleted the class file and recompiled. But now the entire program just does nothing. I'll compile and run, but as soon as I do, whatever I type just appears but nothing will happen – Smo Oct 22 '14 at 16:41
  • And if you rename `repeatCheck` to `randomCheck` what happen? – Ludovic Feltz Oct 22 '14 at 16:45
  • Ahh. I forgot to remove the method call to randomCheck(). Thanks! Got it working. – Smo Oct 22 '14 at 16:50