2
public static int promptUser(Scanner in, String prompt, int min, int max) {
    int userTempVal = 0;

        do {
        userTempVal = in.nextInt();
            if (userTempVal < min || userTempVal > max) {
            System.out.println(prompt);
            }

       }while (userTempVal < min || userTempVal > max);

    return userTempVal; 
    }

This is my current code. I basically need to scan for the user's input and make sure that it's an integer between min and max. If it's not an integer between min and max, it displays the prompt and scans for a new int. My current code fails if the user inputs anything besides an int (so a double or a string). How can I get my code to display the prompt and continue the do while loop when the input is not an int? This is for a class i'm taking and the instructor hinted that we could use the hasNextInt() function to account for this, but every time I try to use it, it either doesn't work or I get an error. Please help, I'm a beginning programmer!

EDIT (Using has Next Int () parameter) :

public static int promptUser(Scanner in, String prompt, int min, int max) {
        int userTempVal = 0;

            do {
            userTempVal = in.nextInt();
                if (userTempVal < min || userTempVal > max || !in.hasNextInt()) {
                System.out.println(prompt);
                }
           }while (userTempVal < min || userTempVal > max || !in.hasNextInt());

        return userTempVal; 
        }

I get this error -> When I type an int, the program stops running. When I type a string: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) at MineTest.promptUser(MineTest.java:60) at MineTest.main(MineTest.java:24)

punchkey
  • 81
  • 1
  • 10
  • "instructor hinted that we could use the hasNextInt() function to account for this, but every time I try to use it, it either doesn't work or I get an error." what error are you getting? What do you mean by "doesn't work"? – Pshemo Oct 25 '17 at 02:55
  • Checking that `hasNextInt()` is `true` is the correct strategy to ensure that a subsequent call to `nextInt()` will successfully return an `int`. It can be made to work. Can you show us how you tried to use it? – Kevin Anderson Oct 25 '17 at 02:59
  • I will make an edit and post my code/the errors I get when I use hasNextInt() – punchkey Oct 25 '17 at 03:01
  • You need to call `hasNextInt()` *before* calling `nextInt()`. Whole point of this method is to determine if it is safe to get data as int. If its result is `false` simply consume that invalid token using other methods which can accept any type of data like `next()` or `nextLine()` and ask for another value. – Pshemo Oct 25 '17 at 03:18
  • How can I do this? I basically want to scan the input, if it's something besides an int, I want to display the 'prompt' and then run the loop again and check if the input is an int (if it's not an int, display the prompt and run again). If it is an int, I want the loop to check if it's a number between the min and max values. – punchkey Oct 25 '17 at 03:20
  • @punchkey I've updated the answer. Try that one. – Shubhendu Pramanik Oct 25 '17 at 03:21

2 Answers2

0

You need to use in.next() to consume the invalid input.

public static int promptUser(Scanner in, String prompt, int min, int max) {
    int userTempVal = 0;

    do {
        if (in.hasNextInt()) {
            userTempVal = in.nextInt();
            if (userTempVal < min || userTempVal > max) {
                System.out.println(prompt);
            }
        } else {
            System.out.println("Invalid input");
            in.next();
        }

    } while (userTempVal < min || userTempVal > max);

    return userTempVal;
}
Shubhendu Pramanik
  • 2,595
  • 2
  • 10
  • 22
-1

Try this:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        System.out.println("Correct Number: " + promptUser(new Scanner(System.in), "give corret number", 10, 20));
    }

    public static int promptUser(Scanner in, String prompt, int min, int max) {
        int userTempVal = 0;

        do {
            if(in.hasNextInt()) { //Check if next input is an Integer
                userTempVal = in.nextInt();
                if (userTempVal < min || userTempVal > max) { //Check boundaries
                    System.out.println(prompt); //Print promt
                }
            }
            else {
                in.next(); // Get the next() Object out of Buffer
                System.out.println(prompt);  //Print promt
            }
        }while ((userTempVal < min || userTempVal > max)); // While is out of bounds
        return userTempVal; 
    }
}

If you have questions, just ask

Tom Stein
  • 297
  • 1
  • 13