How do I compare a variable to see if it is of the correct class type?
I have been learning Java from scratch again since 2 years of rust and I have been playing around with simple random generator code. My problem is that when the user is asked what he wants as his max throw, it must be a class type (int).
I was trying to create an if statement and compare a variable to its class, instead of disabling my IDE and showing me an error message in case the user typed letters.
Here is my code (this is the simplest code, but I'm sure I'm a beginner and motivate myself to 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 the highest roll you want? \n");
highnum = userin.nextInt();
for(int counter=1; counter<= highnum; counter++){
number= 1 + dice.nextInt(highnum);
System.out.println("This is the number " + number);
}
}
}
I want to be able to compare highnum
, here, to see if it remains an int class type rather than a letter. If you enter a letter or symbol, a message should be displayed or the question should be repeated. I tried to find this problem, but I keep getting the results of comparing two variables of the same type.
Is there no way to compare a variable with a class type?
source to share
Java primitive types do not have a class. Their wrapper types do, but your code doesn't use them.
What you are trying to do is check the end user input for character combinations that are an integer and everything else. This is relatively easy to do because it Scanner
provides methods hasNext...
for different data types. You can use hasNextInt()
in a loop, discarding unwanted input, for example:
System.out.println("What the highest roll you want? \n");
while (!userin.hasNextInt()) {
System.out.println("Please enter an integer.");
userin.nextLine();
}
// Since we reached this point, userin.hasNextInt() has returned true.
// We are ready to read an integer from the scanner:
highnum = userin.nextInt();
source to share
nextInt()
(or most other methods nextXYZ
for that matter), throw InputMismatchException
if they encounter an input that doesn't match their invocation (like a letter in the invocation nextInt
). So one option would be to just catch it:
int highnum;
try {
highnum = userin.nextInt();
} catch (InputMismatchException e) {
System.out.println ("Wrong input encountered");
}
source to share
What you are looking for is not a way to "compare a variable to the type of the class", but rather check String
to see if it is in the correct format. If you want to see what String
is only numbers, the easiest way is to use a matches
regular expression as well:
if (inputString.matches("\\d+")) {
... the input is valid
} else {
... complain
}
Regular expression here means "one or more digits". You can also use hasNextInt
in Scanner
or use nextInt
in Scanner
and exclude catch, or use Integer.parseInt(inputString)
and catch exceptions, just to name a few.
source to share