Sunday, 8 September 2013

Java: Overflow and Printing Twice

Java: Overflow and Printing Twice

When I execute this part of my code, I get a StackOverflowError:
/**
* Determine the story based on the time.
*/
private void timeTell()
{
if(replay == 0){
long rndNum = System.currentTimeMillis() % 10;
chooseStory();
}
}
/**
* Randomly choose which story to tell based on the current system time.
*/
private void chooseStory()
{
if(rndNum == 1&& rndNum == 6){
storyOne();
}
else if(rndNum == 2&& rndNum == 7){
storyTwo();
}
else if(rndNum == 3&& rndNum == 8){
storyThree();
}
else if(rndNum == 4&& rndNum == 9){
storyFour();
}
else if(rndNum == 5&& rndNum == 0){
storyFive();
}
else{
timeTell();
}
}
I understand that I do not need the timeTell() method, I'll add it into
the chooseStory() method after I solve this problem. This was just easier
for testing. I tried to figure out where the problem was occuring, so I
replaced chooseStory(); with System.out.println(rndNum); and it prints the
number twice. The variable replay is used to see if the program has
already been run once. If the user decides to play again, replay changes
from the default, 0, to 1 and skip generating a new rndNum. The reason
that I'm using the time instead of a random number generator is because
each time I would run my program, the generator would give me the same
sequence every time. Any help would be greatly appreciated.

No comments:

Post a Comment