java - How to check if user input is not an int value -


i need check if user input value not int value. i've tried different combinations of know either nothing or random errors

for example:

if user inputs "adfadf 1324" it'll raise warning message.


what have:

       // initialize scanner read input command line        scanner sc = new scanner(system.in);        int integer, smallest = 0, input;        boolean error = false;         system.out.print("enter integer between 1-100: ");        range = sc.nextint();         if(!sc.hasnextint()) {            error = true;           system.out.println("invalid input!");           system.out.print("how many integers shall compare? (enter integer between 1-100: ");           sc.next();     }         while(error) {           for(int ii = 1; ii <= integer; ii++) {                ...            } // end loop       }       system.out.println("the smallest number entered was: " + smallest);        }   } 

simply throw exception if input invalid

scanner sc=new scanner(system.in); try {   system.out.println("please input integer");   //nextint throw inputmismatchexception   //if next token not match integer   //regular expression, or out of range   int usrinput=sc.nextint(); } catch(inputmismatchexception exception) {   //print "this not integer"   //when user put other integer   system.out.println("this not integer"); } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -