java - How to compare a variable to see if it's the right class type? -


i've been learning java scratch again since 2 years of rust, , playing around simple random generator code. issue here when user asked wants highest die roll, must number (int) class type.

i trying create if statement , compare variable class, rather letting ide stop , show me error message in case user typed letters.

here code (it's simplest code ever it's safe i'm new , motivating myself learn java again.) :

package firstguy;  import java.util.random; import java.util.scanner;  public class randomnum {   public static void main(string[] args){       random dice = new random();       scanner userin = new scanner(system.in);       int number;       int highnum;        system.out.println("what's highest roll want? \n");       highnum = userin.nextint();          for(int counter=1; counter<= highnum; counter++){           number=  1 + dice.nextint(highnum);           system.out.println("this number " + number);         }        } } 

i want able compare highnum, here see if stays class type int , not letter. in case letter or character typed, message should displayed or question should repeated. i've been trying problem keep getting results of comparing 2 variables of same class type.

is there no way compare variable class type?

primitive types of java not have class. wrapper types do, code not use them.

what trying check end-user input presence of character combinations represent integer vs. else. relatively easy do, because scanner provides methods hasnext... various data types. can use hasnextint() in loop, discarding unwanted input, this:

system.out.println("what's highest roll want? \n"); while (!userin.hasnextint()) {     system.out.println("please enter integer.");     userin.nextline(); } // since reached point, userin.hasnextint() has returned true. // ready read integer scanner: highnum = userin.nextint(); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -