Sameer Kan wrote:For instance, if someone were to enter a double, string, or boolean input into the scanner instead of an integer, how could I throw an exception for this? I have to do this for an assignment, and they specifically said that I need to use a throw exception for this. Thanks everyone in advance!
The Scanner class nextInt() method will throw an InputMismatchException exception, if the token is not an integer. So, you can catch the InputMismatchException instance and throw an IllegalArgumentException upon catching in the catch block.
Now, having said that, if this is a homework assignment, then I don't think your instructor wants you to use the nextInt() method. Instead, your instructor is probably expecting you to use the next() method instead. This method returns a raw token (string), which you can parse out the integer (or throw the exception).
Henry