0

I'm trying to make a short game where the player has to open a treasure chest and buy stuff from a shop, but when i exit the shop, it tells me "Invalid command. Use "shop" or "open."" I added the "end of loop" and found out that after store is finished, it ends the loop and ignores the nextLine() command and goes directly to the if-else ladder. Can anyone help me solve this?

while (win == 0)
    {
        opt = s.nextLine();
        er = 1;
        if (opt.equalsIgnoreCase("shop"))
        {
            store();
            er = 0;
        }
        else if (opt.equalsIgnoreCase("open"))
        {
            gold += open();
        }
        else if (opt.equalsIgnoreCase("exit"))
        {
            return;
        }
        else if (er == 1)
        {
            System.out.println("Invalid command. Use \"Shop\" or \"Open.\"");
            wait (1);
        }
        if (gold >= 1000000)
        {
            win = 1;
        }
        System.out.println ("End of loop");
  • 2
    Please show a [mcve] and read http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo – OneCricketeer Nov 30 '16 at 14:01
  • Please post your code that is before the while loop...Do you have an extra line break that is being eaten up by the `nextLine()`? – brso05 Nov 30 '16 at 14:04
  • Do you read from `s` inside of any of the methods you call? I suspect [this question](http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) is the same problem. – resueman Nov 30 '16 at 14:04

0 Answers0