Java Reference
In-Depth Information
Several of these conversion methods are shown here. Each returns a binary value that cor-
responds to the string.
Wrapper
Conversion Method
Double
static double parseDouble(String
str
) throws NumberFormatException
Float
static float parseFloat(String
str
) throws NumberFormatException
Long
static long parseLong(String
str
) throws NumberFormatException
Integer
static int parseInt(String
str
) throws NumberFormatException
Short
static short parseShort(String
str
) throws NumberFormatException
Byte
static byte parseByte(String
str
) throws NumberFormatException
The integer wrappers also offer a second parsing method that allows you to specify the
radix.
The parsing methods give us an easy way to convert a numeric value, read as a string
from the keyboard or a text file, into its proper internal format. For example, the follow-
ing program demonstrates
parseInt( )
and
parseDouble( )
. It averages a list of numbers
entered by the user. It first asks the user for the number of values to be averaged. It then
reads that number using
readLine( )
and uses
parseInt( )
to convert the string into an
integer. Next, it inputs the values, using
parseDouble( )
to convert the strings into their
double
equivalents.