Enter an integer: 1492 OK Enter an integer: Fourteen ninety two WRONG Enter an integer: 14.92 WRONG Enter an integer: -1492 OK Enter an integer: 1 4 9 2 WRONG
If the user enters one of the WRONG lines,
the statement
inData = stdin.readLine()
inData
.
However, the method
parseInt
in the statement
num = Integer.parseInt( inData )
Enter an integer: 14.92 java.lang.NumberFormatException: 14.92
This exception was passed out of the running program to the Java system, which stopped the program and wrote the error message.
The parseInt
method is just one method of the wrapper class Integer.
It should be used like this:
intVariable = Integer.parseInt( stringVariable );
The stringVariable
should contain only characters that can
be interpreted as an integer.