Skip to content
Advertisement

java: loop with switch only works sometimes

I’m really scratching my heard on this one. I’m new at java, and I’m having the strangest thing happen.

It’s homework and I’m taking it one step at a time. My issue is the loop just keeps going and stops asking for input, just keeps looping until it terminates. My comments are largely for myself. I tried to extract what was causing my problem and post it here.

Look at the “hatColor” switch, you’ll notice the way I’m making sure the user enter only from the options I have allotted. Should I be using a exception handler or something?

Anyway, in short, the problem is that if I enter something with spaces, the loop skips asking for my next input. Like, if I entered “y y y y y ” to the scanner when first prompted, the program will terminate and not give me the chance to enter something else.

Please, anyone that understands this, I would really appreciate your help.

import java.util.Scanner;

public class Testing
{
    static String hatColor;
    public static void main(String[] args) {
    gameStart();    
    }

public static void gameStart() 
{       
    Scanner userInput = new Scanner(System.in);
    boolean keepLooping = true;
    int loopCounter = 0;

    System.out.println("The game begins. You must choose between 3 different colored hats."
            + " You can type white, black, or gray.");

    while (keepLooping == true) 

    {
        hatColor = userInput.next();
        switch(hatColor)
        {
        case "white":
            System.out.println("You have chosen the path of well intentioned decisions.");
            walletDrop();
            //the two items below are only there in case the wallet drop somehow completes without calling another method
            keepLooping = false; // stops the while loop from looping again.
            break; // breaks out of the  switch
        case "gray": 
            System.out.println("You have chosen the path of free will.");
            walletDrop();
            keepLooping = false;
            break;
        case "black" :
            System.out.println("You have chosen the path of personal gain.");
            walletDrop();
            keepLooping = false;
            break;
        default : //we could just copy this default chunk for later switch statements
            if (loopCounter >= 3)//end program on them
            {
                System.exit(0);
            }
            System.out.println("You didn't enter a usable answer. Try again");
            loopCounter++;
            if (loopCounter == 3)
            {
                System.out.println("This program will self destruct if you enter another invalid response.");
            }

        }//end of switch
    }//end of while
}//end of game start method

public static void walletDrop()
{
    System.out.println("wallet drop successful");
}

}

Advertisement

Answer

So I have actually solved this right after posting. In case someone else needs to look here for help:

The issue I was experiencing was due to using the scanner method

variableToAssign = scannerName.next();

instead of

variableToAssign = scannerName.nextLine();

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement