How to check if user input is not int value

I need to check if the input value of the user is not an int value. I tried different combinations of what I know, but I get nothing or random errors

For example:

If the user enters "adfadf 1324", he will cause a warning message.


What I have:

// Initialize a Scanner to read input from the command line Scanner sc = new Scanner(System.in); int integer, smallest = 0, input; boolean error = false; System.out.print("Enter an integer between 1-100: "); range = sc.nextInt(); if(!sc.hasNextInt()) { error = true; System.out.println("Invalid input!"); System.out.print("How many integers shall we compare? (Enter an integer between 1-100: "); sc.next(); } while(error) { for(int ii = 1; ii <= integer; ii++) { ... } // end for loop } System.out.println("The smallest number entered was: " + smallest); } } 
+8
java validation
source share
7 answers

Just throw an exception if the input is invalid

 Scanner sc=new Scanner(System.in); try { System.out.println("Please input an integer"); //nextInt will throw InputMismatchException //if the next token does not match the Integer //regular expression, or is out of range int usrInput=sc.nextInt(); } catch(InputMismatchException exception) { //Print "This is not an integer" //when user put other than integer System.out.println("This is not an integer"); } 
+13
source share
 Try this one: for (;;) { if (!sc.hasNextInt()) { System.out.println(" enter only integers!: "); sc.next(); // discard continue; } choose = sc.nextInt(); if (choose >= 0) { System.out.print("no problem with input"); } else { System.out.print("invalid inputs"); } break; } 
+6
source share

you have the following errors which in turn cause this exception, let me explain this

this is your existing code:

 if(!scan.hasNextInt()) { System.out.println("Invalid input!"); System.out.print("Enter an integer: "); usrInput= sc.nextInt(); } 

in the above code, if(!scan.hasNextInt()) will only become true when user input contains both characters and integers, such as your adfd 123 input.

but you are trying to read only integers inside the if condition using usrInput= sc.nextInt(); . Which is wrong, that throws an Exception in thread "main" java.util.InputMismatchException .

therefore the correct code should be

  if(!scan.hasNextInt()) { System.out.println("Invalid input!"); System.out.print("Enter an integer: "); sc.next(); continue; } 

in the above code, sc.next() will help to read the new input from the user, and continue will help to fulfill the if ( ie if(!scan.hasNextInt()) ) condition again.

Please use the code in your first answer to build your complete logic. If I need any explanation, I know.

+2
source share

try this code [updated] :

 Scanner scan = null; int range, smallest = 0, input; for(;;){ boolean error=false; scan = new Scanner(System.in); System.out.print("Enter an integer between 1-100: "); if(!scan.hasNextInt()) { System.out.println("Invalid input!"); continue; } range = scan.nextInt(); if(range < 1) { System.out.println("Invalid input!"); error=true; } if(error) { //do nothing } else { break; } } for(int ii = 1; ii <= range; ii++) { scan = new Scanner(System.in); System.out.print("Enter value " + ii + ": "); if(!scan.hasNextInt()) { System.out.println("Invalid input!"); ii--; continue; } } 
+2
source share

Taken from a related post :

 public static boolean isInteger(String s) { try { Integer.parseInt(s); } catch(NumberFormatException e) { return false; } // only got here if we didn't return false return true; } 
0
source share

Perhaps you can try the following:

 int function(){ Scanner input = new Scanner(System.in); System.out.print("Enter an integer between 1-100: "); int range; while(true){ if(input.hasNextInt()){ range = input.nextInt(); if(0<=range && range <= 100) break; else continue; } input.nextLine(); //Comsume the garbage value System.out.println("Enter an integer between 1-100:"); } return range; } 
0
source share

This means that the query will be requested while this input is integer, and determine whether it will be odd, or even otherwise it will end.

 int counter = 1; System.out.println("Enter a number:"); Scanner OddInput = new Scanner(System.in); while(OddInput.hasNextInt()){ int Num = OddInput.nextInt(); if (Num %2==0){ System.out.println("Number " + Num + " is Even"); System.out.println("Enter a number:"); } else { System.out.println("Number " + Num + " is Odd"); System.out.println("Enter a number:"); } } System.out.println("Program Ended"); } 
0
source share

All Articles