Java
  Home arrow Java arrow Page 3 - Conversions and Java Print Streams
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Conversions and Java Print Streams
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-06-28

    Table of Contents:
  • Conversions and Java Print Streams
  • Character conversions
  • Format Modifiers
  • Flags
  • Precision

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

      %[argument_index$][flags][width][.precision]conversion

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

    argument_index 

    The number of the argument with which to replace this tag

    flag

    Indicators of various formatting options

    widt

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

    precision

    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);

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Java I/O, Second Edition," published by...
     

    Buy this book now. This article is excerpted from chapter seven of Java I/O, Second Edition, written by Elliotte Rusty Harold (O'Reilly, 2006; ISBN: 0596527500). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT