Akaraka
Akaraka

Reputation: 173

While loop 'ignores' input?

Okay, so here is my code:

public static void playAgain(Scanner in){
    System.out.print("Play again?: ");
    String again=in.next();
    if (again.equalsIgnoreCase("y")){
        playerScore=0;
        aiScore=0;
        playAgain=true;
        }
    else if (again.equalsIgnoreCase("n")){
        playAgain=false;
        }
    else {
        while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n")){
            System.out.print("Invalid response.  Please enter \"y\" or \"n\": ");
            again=in.next();
            }
        }
    }

For some reason, if I input the wrong variable, say 'boog', the while loop prints an error message but for some reason defaults to 'y' even if I input 'n' - for example, a sample run would be:

Play again? boog

Invalid input, please input y or n. n

-program plays again despite my inputting n-

How do I fix this? Is it something with the order? Thanks in advance!

Upvotes: 0

Views: 1907

Answers (5)

jli
jli

Reputation: 6623

Bad flow. You should do something like this (sorry about any mistakes, this was painstakingly typed on my phone :

public static void playAgain(Scanner in){
   System.out.print("Play again?: ");
      while (true) {
         String again=in.next();
         if (again.equalsIgnoreCase("y")){
            playerScore=0;
            aiScore=0;
            playAgain=true;
                break; 
         } else if (again.equalsIgnoreCase("n")){
            playAgain=false;
            break;
         } else {
            // bad input, so allow to loop again
            System.out.print("Invalid input! Please enter y or n");
         } 
      }
}

Upvotes: 0

AusCBloke
AusCBloke

Reputation: 18492

I'm guessing you're using some global variable playAgain to indicate whether or not to play again. I would've returned a boolean from the function but anyway, I don't see playAgain being modified in the last else block:

 else {
        while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n")){
            System.out.print("Invalid response.  Please enter \"y\" or \"n\": ");
            again=in.next();
            }
        }

Perhaps you want the whole set of ifs in a while loop, so again will then be processed after it contains "y" or "n"? Either way the structure of the function needs rethinking.

EDIT: My approach would be something along the lines of:

public static boolean playAgain(Scanner in){
   System.out.print("Play again?: ");
   String again=in.next();

   while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n")) {
      System.out.print("Invalid response.  Please enter \"y\" or \"n\": ");
      again = in.next();
   }

   if (again.equalsIgnoreCase("y")){
      playerScore = 0;
      aiScore = 0;
      return true;
   }

   // there are only two valid options, so to get here it must be "n"
   return false;
}

Upvotes: 4

Naveen Babu
Naveen Babu

Reputation: 1584

as i can see, your program is flawed.

  1. the if-else should be inside another while loop (such as infinite loop or exit in case of again ==n). so that every time you enter the scanner.next() element the play again logic will be executed.
  2. the while loop here executed the code and works fine, since u didnt get the alert "Invalid input, please input y or n." again.

so the value of again was "n" and hence the logic exited of your while loop. so check if playAgain is set as true

Upvotes: 0

Gyan
Gyan

Reputation: 12410

It could be because you are not setting playAgain to false after exiting the while loop. Is the previous value of playAgain true?

I think you need to think about refactoring so that the while loop is above the 'y' and 'n' if/else statement. That way the if/else is always called once the user has entered a valid value. Validation should be at the start of the method.

Upvotes: 3

Daniel Fischer
Daniel Fischer

Reputation: 183883

In the final else branch, after you got valid input from the while loop, you don't set the playAgain variable, so it keeps whixhever value it had - true in this case. You must set it according to the input.

Upvotes: 3

Related Questions