Reputation: 1
I started programming Java 3 Weeks ago, because we started it in School. Now I got a Question, I hope, you can help me!
I want to compare a Variable (in my Case a Integer) to a Data Type:
int a;
(...)
if (a != int) {
System.out.println ("I can only work with Integers")
}
else { }
So everytime, if I type into the Scanner a Char or Double or something different than an Integer, the Program will give a different Output or an Error.
Can you help me, how to compare the Both Types?
Thank you for your answers!
Upvotes: 0
Views: 50
Reputation: 3599
To check a type of variable in Java you can use the instanceof operator.
Example :
if(a instanceof String) {
System.out.println("This is a String!");
}
Upvotes: 1