Java Reference
In-Depth Information
The
Integer
class contains the
valueOf()
and
parseInt()
methods, which are
used to convert strings or
int
types into integers. There are two different forms of the
Integer
class's
valueOf()
type that can be used to convert strings into integer
values. Each of them differs by the number of arguments that they accept. The first
valueOf()
method accepts only a string argument. This string is then parsed as an
integer value if possible, and then an integer holding the value of that string is returned.
If the string does not convert into an integer correctly, then the method will throw a
NumberFormatException
.
The second version of
Integer
's
valueOf()
method accepts two arguments: a
string argument that will be parsed as an integer and an
int
that represents the radix
that is to be used for the conversion.
Note
Many of the Java type classes contain
valueOf()
methods that can be used
for converting different types into that class's type. Such is the case with the
String
class because it contains many different
valueOf()
methods that can be used for con-
version. For more information on the different
valueOf()
methods that the
String
class or any other type class contains, see the online Java documentation at
ht-
There are also two different forms of the
Integer
class's
parseInt()
method.
One of them accepts one argument: the string you want to convert into an integer. The
other form accepts two arguments: the string that you want to convert to an integer and
the radix. The first format is the most widely used, and it parses the string argument as
a signed decimal integer. A
NumberFormatException
will be thrown if a parsable
unsigned integer is not contained within the string. The second format, which is less
widely used, returns an
Integer
object holding the value that is represented by the
string argument in the given radix, given a parsable unsigned integer is contained with-
in that string.
3-7. Iterating Over the Characters of a
String
Problem