Java Reference
In-Depth Information
Note that
Integer
,
Float
,and
Double
are classes that contain methods to convert a
numeric string into a number. These classes are called wrapper classes. Moreover,
parseInt
is a method of the
class
Integer
, which converts a numeric integer
string into a value of the type
int
. Similarly,
parseFloat
is a method of the
class
Float
andisusedtoconvertanumericdecimalstringintoanequivalentvalueof
the type
float
, and the method
parseDouble
is a method of the
class
Double
,
which is used to convert a numeric decimal string into an equivalent value of the
type
double
. At this point, do not be overly concerned with the details of these
classes and methods; just continue to use them as shown previously whenever you
need them. (Chapter 6 discusses these wrapper classes in more detail.)
3
EXAMPLE 3-8
1.
Integer.parseInt("34")
¼
34
Integer.parseInt("-456")
¼
-456
Double.parseDouble("754.89")
¼
754.89
2.
¼
34 + 75
¼
109
Integer.parseInt("34") + Integer.parseInt("75")
Integer.parseInt("87") + Integer.parseInt("-67")
¼
87 - 67
¼
20
3.
Double.parseDouble("754.89") - Double.parseDouble("87.34")
¼
754.89 - 87.34
¼
667.55
Recall that you have already used the
class
Scanner
to input data into a program from
the keyboard, and you used the object
System.out
to output the results to the screen.
Another way to gather input and output results is to use a graphical user interface (GUI).
Java provides the
class
JOptionPane
, which allows the programmer to use GUI
components for I/O. This section describes how to use these facilities to make I/O
more efficient and the program more attractive.
The
class
JOptionPane
is contained in the
package
javax.swing
.Thetwo
methods of this
class
that we use are:
showInputDialog
and
showMessageDialog
.
The method
showInputDialog
allows the user to input a string from the keyboard;
the method
showMessageDialog
allows the programmer to display the results.
The syntax to use the method
showInputDialog
is:
str = JOptionPane.showInputDialog(stringExpression);
Search WWH ::
Custom Search