3.7. Operator Precedence and Associativity

 
[Page 84 ( continued )]

3.6. Formatting Console Output and Strings

You already know how to display console output using the println method. JDK 1.5 introduced a new printf method that enables you to format output. The syntax to invoke this method is

 System.out.printf(format, item1, item2, ..., item  k  ) 

where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, a character, a boolean value, or a string. Each specifier begins with a percent sign. Table 3.8 lists some frequently used specifiers.

Table 3.8. Frequently Used Specifiers
Specifier Output Example
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.460000
%e a number in standard scientific notation 4.556000e+01
%s a string "Java is cool"

Here is an example:


[Page 85]

Items must match the specifiers in order, in number, and in exact type. For example, the specifier for count is %d and for amount is %f . By default, a floating-point value is displayed with six digits after the decimal point. You can specify the width and precision in a specifier, as shown in the examples in Table 3.9.

Table 3.9. Examples of Specifying Width and Precision
Example Output
%5c Output the character and add four spaces before the character item.
%6b Output the boolean value and add one space before the false value and two spaces before the true value.
%5d Output the integer item with width at least 5 . If the number of digits in the item is <5 , add spaces before the number. If the number of digits in the item is >5 , the width is automatically increased.
%10.2f Output the floating-point item with width at least 10 including a decimal point and two digits after the point. Thus there are 7 digits allocated before the decimal point. If the number of digits before the decimal in the item is <7 , add spaces before the number. If the number of digits before the decimal in the item is >7 , the width is automatically increased.
%10.2e Output the floating-point item with width at least 10 including a decimal point, two digits after the point and the exponent part. If the displayed number in scientific notation has width less than 10 , add spaces before the number.
%12s Output the string with width at least 12 characters . If the string item has less than 12 characters, add spaces before the string. If the string item has more than 12 characters, the width is automatically increased.

You can put the minus sign ( ) in the specifier to specify that the item is left-justified in the output within the specified field. For example, the following statement

 System.out.printf(   "%8d%-8s\n"   ,   1234   ,   "Java"   ); System.out.printf(   "%-8d%-8s\n"   ,   1234   ,   "Java"   ); 

displays

 1234Java 1234    Java 

Caution

The items must match the specifiers in exact type. The item for the specifier %f or %e must be a floating-point type value such as 40.0 , not 40 . Thus an int variable cannot match %f or %e .


Tip

The % sign denotes a specifier. To output a literal % in the format string, use %% .


You can print formatted output to the console using the printf method. Can you display formatted output in a message dialog box? To accomplish this, use the static format method in the String class to create a formatted string. The syntax to invoke this method is

 String.format(format, item1, item2, ..., item  k  ) 

This method is similar to the printf method except that the format method returns a formatted string, whereas the printf method displays a formatted string. For example,

 String s = String.format(   "count is %d and amount is %f"   ,   5   ,   45.56   )); 

creates a formatted string "count is 5 and amount is 45.560000" .


[Page 86]

The following statement displays a formatted string in a message dialog box:

 JOptionPane.showMessageDialog(   null   ,   String.format(   "Sales tax is %1.2f"   ,   24.3454   )); 

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net