title: How to get scanner input to not trigger third method?
body: <p>I'm working on an assignment and I mostly have it finished but I am having an issue with the last method. I'm trying to write a <code>continueGame()</code> method that will ask the user if they want to continue to play, and accept "y" or "n". If answered "y", the program starts again. If answered "n", the program stops and a message is shown. The problem is I need it to trigger the <code>continueGame()</code> method only when <code>userChoice == answer</code>. This is a number guessing game with an object oriented approach.</p>
  
  <p>I've tried to call the <code>continueGame()</code> method inside my <code>else if(userChoice == answer)</code> statement but it doesn't seem to work. Even when my other <code>if/else if</code> statements are triggered, it continues to the <code>continueGame()</code> method.</p>
  
  <p>Here is the main driver for the game</p>
  
  <pre><code>import java.util.Scanner;
  
  public class NumberGame
  {
     public static void main(String[] args)
     {
        Scanner input = new Scanner (System.in);
        GameOptions opt = new GameOptions(); // Your created class
        int userChoice = -1234;
        int answer = -1234;
        boolean keepPlaying = true;
  
        System.out.println("Guess the Number Game\n");
  
        while (keepPlaying == true) {
           answer = (int) (Math.random() * 10)+1;
  
           //Create a getChoice method in your class and make sure it accepts a Scanner argument 
           userChoice = opt.getChoice(input);
  
           //Create a checkAnswer method in your class. Make sure it accepts two integer arguments and a Scanner argument
           opt.checkAnswer(userChoice, answer, input);
  
           // Create a continueGame method in  your class and make sure it accepts a Scanner argument
           keepPlaying = opt.continueGame(input);    
        } 
        System.out.println("Thanks for playing.");
     }
  }
  </code></pre>
  
  <p>Here is the class that I am working on for the methods. Note that I can not make any modifications to the main driver file.</p>
  
  <pre><code>import java.util.InputMismatchException;
  import java.util.Scanner;
  import java.lang.NumberFormatException;
  
  public class GameOptions {
      int count = 0;
      boolean cont = true;
      //getChoice Method for NumberGame
      public int getChoice(Scanner scnr) {
  
          System.out.println("Please choose a number between 1 and 10: ");
          int userGuess = 0;
          String input = scnr.next();
          try {
              userGuess = Integer.parseInt(input);
              if (userGuess &lt; 1 || userGuess &gt; 10) {
                  throw new IllegalArgumentException("Invalid value. Please enter a number between 1 and 10: ");
              }
          }
          catch(NumberFormatException e) {
              System.out.println("Error - Enter Numerical Values Only");
              return userGuess;
          }
          catch (IllegalArgumentException ex) {
              System.out.println(ex.getMessage());
          }
          return Integer.parseInt(input);
      }
  
      public void checkAnswer(int userChoice, int answer, Scanner scnr) {
          if (userChoice &gt; answer &amp;&amp; userChoice &lt; 11) {
              System.out.println("Too high. Try again.");
              count++;
          } else if (userChoice &lt; answer &amp;&amp; userChoice &gt; 0) {
              System.out.println("Too low. Try again.");
              count++;
          } else if (userChoice == answer) {
              System.out.println("You got it! Number of tries: " + count);
              System.out.println("Would you like to play again? (y/n)");
          }
      }
  
      public static boolean continueGame(Scanner scnr) {
  
          String input = scnr.nextLine();
          if (input.toLowerCase().equals("y")){
              return true;
          } else if (input.toLowerCase().equals("n")){
              return false;
          } else {
              System.out.println("Invalid entry. Please enter either y or n: ");
              return continueGame(scnr);
          }
      }
  }
  </code></pre>
  
  <p>So I should be able to enter a number, and if its lower than the answer it will tell me I am too low, if its higher than the answer it will tell me that its too high, if its equal it will tell me I won and prompt me to press "y" or "n" if I want to continue. Another issue I am running into is that I am getting "Would you like to play again? (y/n)" no matter whether I guess the right number or not and my only option is to hit "y" or "n"</p>