I'm currently working on a program that accepts integer values at one point (using Scanner method nextInt). Of course, if you type "one" instead of "1", the program crashes. I'm trying to get it instead to just to say that it was an invalid input and to try again if it isn't an integer, and continue if the value is an integer. So far I'm using the Scanner method hasNextInt, which determines if the value is an integer and returns a boolean value, but the part I'm having trouble with is continuing if an integer value was inputted. So is it possible to continue the program with the inputted value without having to ask for it again?
3 Answers
if (x == (int)x)
{
// Number is integer
}
and try that too
Object x = someApi();
if (x instanceof Integer)
How can I check if a value is of type Integer? and How to check if the value is integer in java?
if (scanner.hasNextInt()) { i = scanner.nextInt();}not acceptable for your case?