Search code examples
while-loopuser-input

Why does this ask for user input twice?


public static void main(String[] args) {

    Scanner userInput = new Scanner(System.in);
    String choice = "";
    while(true) {
        System.out.println("What would you like to do? Enter help for commands.");
        choice = userInput.nextLine();
        switch(choice.toLowerCase()) {
            case "turn right":
                System.out.println("right");
            case "turn left":
                System.out.println("left");
            case "move":
                System.out.println("move");
        }
    }

On each iteration of the loop, it asks for user input two times. For example, it requires two lines before reaching the switch statement. Why does this happen?


Solution

  • Eclipse glitched and my code ran improperly. Fixed by closing and reopening Eclipse.