I am requesting users input where he needs to write integers. I managed to create validation that checks if the value is higher than needed and so, with this code :
int n = sca.nextInt();
while (n<=0){
System.err.println(error_1);
n = sca.nextInt();
}
But now how to add check for strings, I found such solution How do I keep a Scanner from throwing exceptions when the wrong type is entered?
That uses hasNextInt()
before actually reading the input, I tried to put this check inside while loop in the same place with n<=0
like this
while ( (n<=0)||(sca.hasNextInt() )) {
....
}
But it responded with error that variable n
is not compatible with that method.
So is there any way to overcome such thing?