-2

In my While loop after the first loop it does break out of the While loop but it does not step into my If check, does anyone see what I've done wrong?

    while (!"Y".equals(inputString) && !"N".equals(inputString)) {
        inputString = Helper.InputHelper().toUpperCase();
        if (inputString.equals("N")) {
            System.out.println("Thank you for playing!");
            System.exit(0);
        } else if (inputString.equals("Y")) {
            System.out.println("Starting a new game");
            MineSweeper game;
            game = new MineSweeper();
        }
    }

Everyone is right I'm braindead, changed it to

System.out.println("Would you like to play another game? (Y/N): ");
    String input = "";
    while (!"Y".equals(input) && !"N".equals(input)) {
        input = Helper.InputHelper().toUpperCase();
        if (input.equals("N")) {
            System.out.println("Thank you for playing!");
            System.exit(0);
        } else if (input.equals("Y")) {
            System.out.println("Starting a new game");
            MineSweeper game;
            game = new MineSweeper();
        }
    }

and works flawless thank you.

Strah Behry
  • 551
  • 2
  • 8
  • 25

4 Answers4

0

You must repeat reading userinput inside the while-loop. Normally this is a do-while-loop .

String inputString = null;
do
{
    inputString = new Scanner(System.in).next();
    System.out.println("inputString = " + inputString);
}
while (!"Y".equals(inputString) && !"N".equals(inputString));
Tobias Otto
  • 1,634
  • 13
  • 20
  • he doesn't need to. he only gets to that loop after the input, then he want to check whether it is Y or N – Ahad Oct 27 '16 at 13:45
  • I think the loop around the "read from keyboard" is needet, because the user may type any letter. When the user type the letter 'z' than the loop continues until the user has entered a valid input. Valid input is only 'Y' and 'N'. – Tobias Otto Oct 27 '16 at 13:50
0
while (!"Y".equals(inputString) && !"N".equals(inputString)) 

you have problem in the while loop condition .

you tell the while if the inputString NOT equal Y AND not equal N then enter the while !

instead you should enter the loop if the inputString equal Y OR equal N.

else you need the user to keep enter the inputString

0

i bet this is what you want.

Scanner input = new Scanner(System.in);
    while (true) {        
        String answer = input.next().toUpperCase();
        if (answer.equals("N")) {
            System.out.println("Thank you for playing!");
            // don't forget to close you scanner
            input.close();
            System.exit(0);
            break;
        } else if (answer.equals("Y")) {
            System.out.println("Starting a new game");
            MineSweeper game;
            game = new MineSweeper();
            input.close();
            break;
        } else {
            // if you don't get appropriate answer from users then warn them about it
            System.out.println("Please type either Y or N");
        }
    }

either way, when you hit the condition, you should break, otherwise there is no stooping that loop.

Ahad
  • 674
  • 1
  • 9
  • 22
0

It is always recommended for you to make some test with values, for example:

if inputString = "N" The while part would evaluate to (!"Y".equals("N") && !"N".equals("N")) so the left part is true and the right part false and the AND makes it all false and the while condition is not true whether the user puts N or Y. I think you should simplify your while condition by using the || (OR) operator and removing the NOT (!) operator.