Java. In brackets you see what I want to do.
if (variableThatIsAnInt != [intType])
System.out.println("invalid option");
Java. In brackets you see what I want to do.
if (variableThatIsAnInt != [intType])
System.out.println("invalid option");
A variable which is an int
or Integer
can never be anything else than this type. This is only possible if you ditch the typing completely and define everything as an Object
only.
As an example:
Integer myVar = 1;
Object myDirtyVar = myVar;
A check whether myVar is an instance of Integer would always be true.
What you are actually looking for is to check whether a String value is assignable to a numerical (in this case: Integer) number.
This can be done multiple ways, one way would be the following:
What's the best way to check if a String represents an integer in Java?
Another way is to try to parse/convert it and then catch the Exception (NumberFormatException) which may appear if the String
is not representing an Integer
.