[sorry if this is a stupid question, I had a lot of trouble wording it for a google search, and couldn't find any answers to the question, even though it'll probably be like a one-line answer]
I'm just following some tutorials on my Java programming course's website.
Right now I'm coding something that takes will open a prompt screen and be like, "Please enter an integer between 5 and 10: ", and scan the number, and do that for a bunch of different variable types, different boundaries, etc.
Super easy stuff. I just have one small question.
How do I tell my scanning method to check that the variable is of type int? I know it's probably really obvious, but if someone enters "A" or "1.543," I'm supposed to have it display, "That is not an integer!" but I don't know how to actually use the scanner to check variable types
Firstly, because you want to do validation, just read the input in as a String.
Then you want to check that it's a number. You can use the
parse
method, wrapped up in atry catch
.And if you want to do it in a loop, add this
try catch
to a method that returns aboolean
..