0

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!

2
  • Your code says Invalid response, but your screen protocol says Invalid input. That doesn't match. Besides that, I would make the method return playAgain directly, instead of saving it into a field variable. The fewer fields you have in a class, the easierr it is to understand. Commented Nov 1, 2011 at 3:06
  • I edited my answer if you want an alternative to your current structure of things. Commented Nov 1, 2011 at 3:16

5 Answers 5

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had a similar problem and found this via google. Thank you @AusCBloke. +1
3

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.

Comments

3

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.

Comments

0

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

Comments

0

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");
         } 
      }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.