-2

I seem to have my scanner/Eclipse auto running user input. I have a scanner initialised as:

Scanner in = new Scanner(System.in);

with a menu as:

@Override
void update() {
    //welcome
    System.out.println("-----Welcome to the Pet Game-----");
    System.out.println();
    System.out.println("1. Play");
    System.out.println("2. Help");
    System.out.println("3. Dev Mode");
    int choice = in.nextInt();
    switch(choice){
    case 1:
        playLoop();
        break;
    case 2:
        helpPage();
        break;
    case 3:
        devMode();
        break;
    }

}

For some reason it is automatically inputting 2 on this menu. And follows with 3 on this menu:

// main menu
        System.out.println("This is Day " + (i+1) + " you have " + dayActions + " Actions left.");
        System.out.println("1. View Pet Status");
        System.out.println("2. Shop");
        System.out.println("3. Skip Action");
        System.out.println("4. Play");
        System.out.println("5. Feed");
        System.out.println("6. Bathroom");
        System.out.println("7. Sleep");

        int choice = in.nextInt();
        System.out.println();

I think this might be due to my GameState class but I am not sure. Gamestate class:

abstract class GameState {

    abstract void update();

    protected void pushState(GameState state) {
        PetGame.pushState(state);
    }

    protected void popState() {
        PetGame.popState();
    }
}

StoreState (This is the class it is jumping to. I can't find any reference to it in my Code no idea why it is going here.)

public class StoreState{

    @Override
    void update() {
        // TODO Auto-generated method stub

    }


}

Thanks for your help.

T. Kearsley
  • 197
  • 3
  • 18
  • please **don't** update your question as your problems are being solved. That is completely changing the context of the initial problem. you're not doing anyone any favours here by doing that.rather append an edit section below your original problem stating the new problem you're facing. – Ousmane D. Apr 25 '17 at 13:21
  • @OusmaneMahyDiaw Your intention is generally correct, but *"rather append an edit section below your original problem stating the new problem you're facing"* is wrong. A new issue must be asked in a new post/question (this has been asked and discussed several times on meta, for example here: https://meta.stackoverflow.com/questions/298948/question-answered-but-a-new-issue-appeared) – Tom Apr 25 '17 at 13:47
  • @Tom thanks, still learning the system you see ^^. Appreciated. – Ousmane D. Apr 25 '17 at 13:50
  • @OusmaneMahyDiaw You're welcome. – Tom Apr 25 '17 at 13:52

1 Answers1

-1

you'll need to utilise the break statement to jump out of the switch statement after a particular case has been met, otherwise, you'll get something called a fallthrough.

case 1:
    playLoop();
    break;
case 2:
    helpPage();
    break;
case 3:
    devMode();
    break;
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • First: this question is a dupe, so don't answer it (flag it instead). And second: a `break` right after the `case` and before the actual method call? – Tom Apr 25 '17 at 12:47
  • I call it: don't post bad answers. – Tom Apr 25 '17 at 12:50
  • I've added them but it seems to still be stopping due to possibly StoreState? – T. Kearsley Apr 25 '17 at 12:51
  • There are no references to Store state now (I've now removed it from being a subclass of GameState) but it is still jumping back to StoreState. I've tried restarting my IDE no fix. – T. Kearsley Apr 25 '17 at 12:58