What I would do is get the String input and parse it as a double or integer.
String str = input.next(); int i = 0; double d = 0d; boolean isInt = false, isDouble = false; try {
This way you can ensure that you don't lose integer precision by simply parsing the double (doubles a different range of possible values ββthan integers), and you can handle cases where neither a real integer nor a double has been entered.
If an exception is thrown by code inside the try block, the code in the catch block is executed. In our case, if the exception is thrown by the parseInt() method, we execute the code in the catch block, where the second try block is located. If the exception is thrown by the parseDouble() method, then we execute the code inside the second catch block, which displays an error message.
source share