Java Reference
In-Depth Information
The output of this program is similar to the output of the program in Example 3-5.
Here, the output is left justified. Notice that in the Sample Run, Lines 2 through 8
contain *** . This is to show how the value of the last argument is printed. The
details are left as an exercise for you.
Soon, we will explain how to use input/output dialog boxes to input data into a
program and then display the output of the program. However, input to a program
using input dialog boxes is in string format. Even numeric data is input as strings.
Therefore, you first need to learn how to convert numeric strings, called parsing
numeric strings, into numeric form.
PARSING NUMERIC STRINGS
A string consisting of only an integer or a floating-point number, optionally preceded by
aminussign,iscalledanumeric string. For example, the following are numeric strings:
"6723"
"-823"
"345.78"
"-782.873"
To process these strings as numbers for addition or multiplication, we first must convert
them into numeric form. Java provides special methods to convert numeric strings into
their equivalent numeric form.
1. To convert a string consisting of an integer to a value of the type int ,
we use the following expression:
Integer.parseInt(strExpression)
For example:
Integer.parseInt("6723") = 6723
Integer.parseInt("-823") = -823
2. To convert a string consisting of a floating-point number to a value of
the type float , we use the following expression:
Float.parseFloat(strExpression)
For example:
Float.parseFloat("34.56") = 34.56
Float.parseFloat("-542.97") = -542.97
3. To convert a string consisting of a floating-point number to a value of
the type double , we use the following expression:
Double.parseDouble(strExpression)
For example:
Double.parseDouble("345.78") = 345.78
Double.parseDouble("-782.873") = -782.873
Search WWH ::




Custom Search