Java Reference
In-Depth Information
str = String.format("The value of x with two decimal "
+ "places = %.2f%n", x)
+ String.format("The value of y with two decimal "
+ "places = %.2f%n", y)
+ String.format("The value of z with two decimal "
+ "places = %.2f%n", z);
JOptionPane.showMessageDialog(
null
, str,
"Formatting with the String Method format",
JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
Sample Run: (Figure 3-13 shows the output of this program.)
FIGURE 3-13
Output dialog box showing the values of
x
,
y
, and
z
with two decimal places
Note that in the preceding program, first we constructed
str
using the
String
method
format
and then used
str
in the output dialog box. However, you could have used the
String
method
format
directly in the output dialog box. That is, you can replace the
statements:
str = String.format("The value of x with two decimal "
+ "places = %.2f%n", x)
+ String.format("The value of y with two decimal "
+ "places = %.2f%n", y)
+ String.format("The value of z with two decimal "
+ "places = %.2f%n", z);
JOptionPane.showMessageDialog(
null
, str,
"Formatting with the String Method format",
JOptionPane.INFORMATION_MESSAGE);
with the following statement:
JOptionPane.showMessageDialog(
null
,
String.format("The value of x with two decimal "
+ "places = %.2f%n", x)
Search WWH ::
Custom Search