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)

6
  • "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"? Commented Oct 25, 2017 at 2: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? Commented Oct 25, 2017 at 2:59
  • I will make an edit and post my code/the errors I get when I use hasNextInt() Commented Oct 25, 2017 at 3: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. Commented Oct 25, 2017 at 3: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. Commented Oct 25, 2017 at 3:20

2 Answers 2

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

3 Comments

Thank you! This works, but now if I type a string that is multiple words, it outputs the prompt 3 times. How can I fix this?
Nevermind! I just changed in.next() to in.nextLine() !
And What if you type the end-of-line (windows <ctrl>z, linux/macos <ctrl>d), jeje , that will show you an Error.
-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.