Search code examples
javajava.util.scannerminesweeper

Loop printing 2 errors instead of 1 for minesweeper


Below you'll find method specification and the method I wrote to match those:

/**
 * This method prompts the user for a number, verifies that it is between min
 * and max, inclusive, before returning the number.  
 * 
 * If the number entered is not between min and max then the user is shown 
 * an error message and given another opportunity to enter a number.
 * If min is 1 and max is 5 the error message is:
 *      Expected a number from 1 to 5.  
 * 
 * If the user enters characters, words or anything other than a valid int then 
 * the user is shown the same message.  The entering of characters other
 * than a valid int is detected using Scanner's methods (hasNextInt) and
 * does not use exception handling.
 * 
 * Do not use constants in this method, only use the min and max passed
 * in parameters for all comparisons and messages.
 * Do not create an instance of Scanner in this method, pass the reference
 * to the Scanner in main, to this method.
 * The entire prompt should be passed in and printed out.
 *
 * @param in  The reference to the instance of Scanner created in main.
 * @param prompt  The text prompt that is shown once to the user.
 * @param min  The minimum value that the user must enter.
 * @param max  The maximum value that the user must enter.
 * @return The integer that the user entered that is between min and max, 
 *          inclusive.
 */

public static int promptUser(Scanner in, String prompt, int min, int max) {
    //initialize variables
    Integer userInput = 0;
    boolean userInteger = false;
    System.out.print(prompt);//prompts the user for input
    userInteger = in.hasNextInt();
    while (userInteger == false) {
            System.out.println("Expected a number from " + min + " to " + max +".");     
        in.nextLine();
        userInteger = in.hasNextInt();
    }

    while (userInteger == true) {
        userInput = in.nextInt();
        while (userInput > max || userInput < min) {
            System.out.println("Expected a number from " + min + " to " + max +".");
            in.nextLine();
            userInteger = in.hasNextInt();

            while (userInteger == false) {
                System.out.println("Expected a number from " + min + " to " + max +".");
                in.nextLine();
                userInteger = in.hasNextInt();

            }
            userInput = in.nextInt();                   
        }
        userInteger = false;
   }
   //userInteger = false; 
   return userInput; //FIXME
}

unfortunately when I try to test with the following values : 4 4 5 4 yo yo yo

I get two errors printed instead of 1 when I type in yo yo. I know I'm printing the same print statement twice and it's the print statement under the while (userInteger = false) loop. Any thoughts on how to fix this?


Solution

  • This is what happens when you have "yo yo" as an input. Scanner.nextInt() takes in the first "yo" as an input and prints your error message, and the second "yo" stays in the input buffer. When it calls .nextInt() the second time, it takes in the second "yo" which was in the buffer, and prints out another error output.

    You might reconsider using Scanner.nextLine() and then parsing the input instead of simply using Scanner.nextInt().

    Hope this helps.