简体   繁体   中英

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.

You are using the wrong boolean operator.

rndNum == x && rndNum == y

Will pass if and only if rndNum is equal to both x AND y - this will never happen if x and y are different values. You need to use the OR operator, ' || ':

rndNum == X || rndNum == y

Also, as @Sotirios Delimanolis pointed out, you have variable masking going on as well. He has also explained why the StackOverflowError is happening

If you only have one variable rndNum it can only be assigned one value at a time. So rndNum == 1&& rndNum == 6 and all the following if s will be false. This is why timeTell() will always be called.

First rndNum is local to to timetell. So probably instance level rndNum is not initialized.

But even if it were initialized properly your conditional check has to be OR not AND. A variable can have only one value at a 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();
}

So currently you have all condition getting wrong and control reaches else block everytime , and thus endless recursive call to timetell and hence the stackOverFlow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM