Page 207 - Introduction to Programming with Java: A Problem Solving Approach
P. 207

                5.7 Formatted Output with the printf Method 173 then for formatted output. It’s the printf method, where the “f” stands for “formatted.” We describe the
printf method in this section. Formatted Output
For most programs, the goal is to calculate something and then display the result. It’s important that the dis- played result is understandable. If it’s not understandable, then no one will bother to use the program, even if it calculates flawlessly. One way to make your displayed results understandable is to format your output. By that, we mean having data columns align properly, having floating-point numbers show the same number of digits after the decimal point, and so on. Note the formatting in the budget report below. The left column is left-aligned. The other columns are right aligned. The numbers show two digits at the right of the decimal point. The numbers show commas between every third digit at the left of the decimal point. And finally, the numbers show parentheses to indicate negativeness.
Account ActualBudgetRemaining
------- ---------------------
Office Supplies
1,150.00
1,400.00
250.00
Photocopying 2,100.112,000.00(100.11)
Total remaining: $149.89
The System.out.printf method is in charge of generating formatted output. The
printf method has lots of formatting features. We’ll keep things simple and explain only
a few of the more popular features. We begin our explanation of the printf method by
showing you how to generate the “Total remaining” line in the above budget report. Here’s
  Learn how to use versatile tools.
 the code:
System.out.printf(
format specifier
Apago PDF Enhancer
    "\nTotal remaining: $%.2f\n", remaining1 + remaining2);
The printf method’s first argument is known as the format string. It contains text that prints as is, plus format specifiers that handle formatted printing. In the above example, “\nTotal remaining: $...\n” is the text that prints as is. And %.2f is the format specifier. Think of a format specifier as a hole where you plug in a
data item. In the above example, remaining1
remaining1 holds 250 and remaining2 holds 􏰂100.11, the sum is 149.89 and 149.89 gets plugged into the format specifier hole. The format specifier starts with % because all format specifiers must start with %. The format specifier’s .2 causes two digits to be displayed after the decimal point. The format specifier’s f indicates that the data item is a floating-point number. The example shows only one format specifier. You can have as many format specifiers as you like in a given format string. For each format specifier, you should have a corresponding data item/argument. Here’s an illustration of what we’re talking about:
+
remaining2 is the data item that gets plugged in. If
  format string
           System.out.printf("<text> %
format specifier holes
<text>", <item1>, <item2>);
<text> %
             



































































   205   206   207   208   209