Using Flags in the printf Format String

Various flags may be used with method printf to supplement its output formatting capabilities. Seven flags are available for use in format strings (Fig. 28.14).

Figure 28.14. Format string flags.

(This item is displayed on page 1338 in the print version)

Flag

Description

- (minus sign)

Left justify the output within the specified field.

+ (plus sign)

Display a plus sign preceding positive values and a minus sign preceding negative values.

space

Print a space before a positive value not printed with the + flag.

#

Prefix 0 to the output value when used with the octal conversion character o.

 

Prefix 0x to the output value when used with the hexadecimal conversion character x.

0 (zero)

Pad a field with leading zeros.

, (comma)

Use the locale-specific thousands separator (i.e., ',' for U.S. locale) to display decimal and floating-point numbers.

(

Enclose negative numbers in parentheses.

To use a flag in a format string, place the flag immediately to the right of the percent sign. Several flags may be used in the same format specifier. Figure 28.15 demonstrates right justification and left justification of a string, an integer, a character and a floating-point number. Note that line 9 serves as a counting mechanism for the screen output.

Figure 28.15. Right justifying and left justifying values.

(This item is displayed on page 1339 in the print version)

 1 // Fig. 28.15: MinusFlagTest.java
 2 // Right justifying and left justifying values.
 3
 4 public class MinusFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.println( "Columns:" );
 9 System.out.println( "0123456789012345678901234567890123456789
" );
10 System.out.printf( "%10s%10d%10c%10f

", "hello", 7, 'a', 1.23 );
11 System.out.printf( 
12  "%-10s%-10d%-10c%-10f
", "hello", 7, 'a', 1.23 );
13 } // end main
14 } // end class MinusFlagTest
 
Columns:
0123456789012345678901234567890123456789

 hello 7 a 1.230000

hello 7 a 1.230000
 

Figure 28.16 prints a positive number and a negative number, each with and without the + flag. Note that the minus sign is displayed in both cases, but the plus sign is displayed only when the + flag is used.

Figure 28.16. Printing numbers with and without the + flag.

 1 // Fig. 28.16: PlusFlagTest.java
 2 // Printing numbers with and without the + flag.
 3
 4 public class PlusFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%d	%d
", 786, -786 );
 9 System.out.printf( "%+d	%+d
", 786, -786 );
10 } // end main
11 } // end class PlusFlagTest
 
786 -786
+786 -786
 

Figure 28.17 prefixes a space to the positive number with the space flag. This is useful for aligning positive and negative numbers with the same number of digits. Note that the value -547 is not preceded by a space in the output because of its minus sign. Figure 28.18 uses the # flag to prefix 0 to the octal value and 0x to the hexadecimal value.

Figure 28.17. Using the space flag to print a space before non-negative values.

(This item is displayed on page 1340 in the print version)

 1 // Fig. 28.17: SpaceFlagTest.java
 2 // Printing a space before non-negative values.
 3
 4 public class SpaceFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "% d
% d
", 547, -547 );
 9 } // end main
10 } // end class SpaceFlagTest
 
 547
-547
 

Figure 28.18. Using the # flag with conversion characters o and x.

(This item is displayed on page 1340 in the print version)

 1 // Fig. 28.18: PoundFlagTest.java
 2 // Using the # flag with conversion characters o and x.
 3 
 4 public class PoundFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 int c = 31; // initialize c
 9
10 System.out.printf( "%#o
", c );
11 System.out.printf( "%#x
", c );
12 } // end main
13 } // end class PoundFlagTest
 
037
0x1f
 

Figure 28.19 combines the + flag, the 0 flag and the space flag to print 452 in a field of width 9 with a + sign and leading zeros, next prints 452 in a field of width 9 using only the 0 flag, then prints 452 in a field of width 9 using only the space flag.

Figure 28.19. Printing with the 0 (zero) flag fills in leading zeros.

(This item is displayed on pages 1340 - 1341 in the print version)

 1 // Fig. 28.19: ZeroFlagTest.java
 2 // Printing with the 0 (zero) flag fills in leading zeros.
 3
 4 public class ZeroFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%+09d
", 452 );
 9 System.out.printf( "%09d
", 452 );
10 System.out.printf( "% 9d
", 452 );
11 } // end main
12 } // end class ZeroFlagTest
 
+00000452
000000452
 452
 

Figure 28.20 use the comma (,) flag to display a decimal and a floating-point number with the thousands separator.Figure 28.21 encloses negative numbers in parentheses using the ( flag. Note that the value 50 is not enclosed in parentheses in the output because it is a positive number.

Figure 28.20. Using the comma (,) flag to display number with thousands separator.

(This item is displayed on page 1341 in the print version)

 1 // Fig. 28.20: CommaFlagTest.java
 2 // Using the comma (,) flag to display numbers with thousands separator.
 3
 4 public class CommaFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%,d
", 58625 );
 9 System.out.printf( "%,.2f", 58625.21 );
10 System.out.printf( "%,.2f", 12345678.9 );
11 } // end main
12 } // end class CommaFlagTest
 
58,625
58,625.21
12,345,678.90
 

Figure 28.21. Using the ( flag to place parentheses around negative numbers.

(This item is displayed on page 1341 in the print version)

 1 // Fig. 28.21: ParenthesesFlagTest.java
 2 // Using the ( flag to place parentheses around negative numbers.
 3
 4 public class ParenthesesFlagTest
 5 {
 6 public static void main( String args[] )
 7 {
 8 System.out.printf( "%(d
", 50 );
 9 System.out.printf( "%(d
", -50 );
10 System.out.printf( "%(.1e
", -50.0 );
11 } // end main
12 } // end class ParenthesesFlagTest
 
50
(50)
(5.0e+01)
 

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

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