Home arrow Java arrow Page 3 - Conversions and Java Print Streams

Conversions and Java Print Streams

In the conclusion to this three-part article, we'll discuss data and time conversions, character conversions, and more. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
June 28, 2007
  1. · Conversions and Java Print Streams
  2. · Character conversions
  3. · Format Modifiers
  4. · Flags
  5. · Precision

print this article

Conversions and Java Print Streams - Format Modifiers
(Page 3 of 5 )

In addition to a conversion code, the format string can also specify a width, a precision, the argument itís replaced with, and any of several special-purpose flags. The most general format follows this pattern:


Here is a quick definition of those parameters. More detail on each will follow:


The number of the argument with which to replace this tag


Indicators of various formatting options


The minimum number of characters with which to  format the replacement value


The number of characters after the decimal point; alternately, the maximum number of characters in the formatted string

These four options control exactly how a string is formatted as tags are replaced by values.

Argument index

The argument index is specified when the order of the arguments does not match the order of the tags. For example:

  out.printf("There are %2$f centimeters in %1$f feet.", feet, 2.54 * feet * 12);

In this case, indexes start with 1 rather than 0, which is unusual for Java. (The format string counts as argument 0.) If you reference a nonexistent argument,printf()throws aMissingFormatArgumentException.

The argument index is particularly useful when you want to repeat the same value more than once in a string, perhaps formatted differently each time. For example:

  System.out.printf("Hexadecimal: %1$H Decimal: %1$f", Math.PI);

You can also repeat the previous argument by specifying a less than sigh (<) rather than an integer and a dollar sign ($). For instance, this statement is equivalent to the previous statement:

  System.out.printf("Hexadecimal: %1$H Decimal: %<f", Math.PI);

Next: Flags >>

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials