0

Here is my code. it is a simple console application intended to get me familiar with object use. `package okei; import java.util.Scanner;

public class Main {

public static void main(String[] args)
{
    Scanner listen = new Scanner(System.in);

    Arg dec = new Arg();
    String answer = "";
    int howmuch;
    Boolean a = true;
    while(a)
    {   

        System.out.println("Drink, Fill, or Look?");
        answer = listen.nextLine();

        switch (answer)
        {
        case "Drink":
            System.out.println("How much?");
            howmuch = listen.nextInt();
            dec.drink(howmuch);
            break;
        case "Fill":
            System.out.println("How much?");
            howmuch = listen.nextInt();
            dec.fill(howmuch);
            break;
        case "Look":
            System.out.println(dec.look());
            break;
        case "Quit":
            listen.close();
            a=false;
            break;
        }

    }

}

} `

Here is the ouptut Drink, Fill, or Look? Drink How much? 10 Drink, Fill, or Look? Drink, Fill, or Look?

Why is Drink, Fill, or Look being executed twice?

CLASSIFIED
  • 13
  • 5

1 Answers1

0

Just change "answer = listen.nextLine()" to "answer = listen.next()"

  • It's not executing twice, it's just skipping Scanner#nextLine every time when you are using Scanner#nextInt before it, that's because the Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine. Here is the [answer link](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Ruben Hakobyan Nov 04 '15 at 07:40