Integer.parseInt(String) throws an exception when it encounters some characters that cannot be converted into integers.
An example
str = BF . readline(); //bf is a buffer stream and str is a string.
num = integer . parse int(str); //Here, the str string is converted into an integer and stored in the num of type int.
Its function is that if the read str is not an integer, it will report an error and can throw an exception to solve this problem.
Please try {str = brd.readline ();
num = integer . parse int(str);
}
Capture (exception e)
{
System.out.println ("Sorry, only integers can be entered, please re-enter." ) ;
}
In this way, when you enter non-integer characters such as h and a again, you will be prompted to enter an integer.