Java Reference
In-Depth Information
As we have seen in the previous chapters, as an input to a dialog box or a text field, Java
programs accept only strings as input. Numbers, integer or decimal, are entered as
strings. We then use the method
parseInt
of the
class
Integer
to convert an
integer string into the equivalent integer. If the string containing the integer contains
only digits, the method
parseInt
will return the integer. However, if the string
contains a letter or any other nondigit character, the method
parseInt
throws a
NumberFormatException
. Similarly, the method
parseDouble
also throws this
exception if the string does not contain a (valid) number. The programs that we've
written up to this point ignored these exceptions. Later in this chapter we show how to
handle these and other exceptions.
Tables 11-10, 11-11, and 11-12 list some of the exceptions thrown by the methods of the
class
es
Integer
,
Double
, and
String
.
TABLE 11-10
Exceptions Thrown by the Methods of the
class
Integer
Method
Exception Thrown
Description
The string
str
does not contain
an
int
value.
parseInt(String str)
NumberFormatException
The string
str
does not contain
an
int
value.
valueOf(String str)
NumberFormatException
TABLE 11-11
Exceptions Thrown by the Methods of the
class
Double
Method
Exception Thrown
Description
The string
str
does
not contain
a
double
value.
parseDouble(String str)
NumberFormatException
The string
str
does
not contain
a
double
value.
valueOf(String str)
NumberFormatException
Search WWH ::
Custom Search