Home : Course Map : Chapter 9 : Java :
Formatter Class and printf() - J2SE5.0
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

In Chapter 5: Tech we introduced the new printf() method that was added to the PrintStream class by J2SE 5.0. Internally it uses the java.util.Formatter class to format numerical values with specifiers similar to those used for the printf() function in the C language. The java.util.Formatter class includes the method

  format (String format, Object... args)

This is virtually identical to the printf() form in PrintStream as we discussed in Chapter 5. The args parameters will be displayed in the output according to the specifiers in the format string in the first parameter.

For a detailed description of the format specifiers, see the printf() discussion in Chapter 5: Tech and the Java 2 API Specifications for java.util.Formatter.

The java.util.Formatter class provides several constructors, each of which includes a parameter for the destination of the formatted output. Destinations include OutputStream, an instance of File, and any class that implements the new Appendable interface.

The program FormatWriteApp shows how we can use Formatter to send formatted numerical values to the console rather than using the printf() method.

FormatWriteApp.java

import java.io.*;
import java.util.*;

/**
  * Demonstrate the java.util.Formatter capabilities for
  * formatting primitive types.
**/
public class FormatWriteApp
{
  public static void main (String arg[]) {

    // Send formatted output to the System.out stream.
    Formatter formatter  =
      new Formatter ((OutputStream)System.out);

    formatter.format ("Text output with Formatter. %n");
    formatter.format ("Primitives converted to strings: %n");

    boolean a_boolean =  false;
    byte    a_byte    =  114;
    short   a_short   =  1211;
    int     an_int    =  1234567;
    long    a_long    =  987654321;
    float   a_float   =  983.6f;
    double  a_double  = -4.297e-15;

    formatter.format ("boolean = %9b %n",   a_boolean);
    formatter.format ("byte    = %9d %n",   a_byte);
    formatter.format ("short   = %9d %n",   a_short);
    formatter.format ("int     = %9d %n",   an_int);
    formatter.format ("long    = %9d %n",   a_long);
    formatter.format ("float   = %9.3f %n", a_float);
    formatter.format ("double  = %9.2e %n", a_double);

    // Need to flush the data out of the buffer.
    formatter.flush ();
    formatter.close ();

  } // main

} // class FormatWriteApp

The output of this program look like:

Text output with Formatter.
Primitives converted to strings:
boolean =     false
byte    =       114
short   =      1211
int     =   1234567
long    = 987654321
float   =   983.600
double  = -4.30e-15

In the Formatter constructor, the System.out argument, which references an instance of PrintStream, must be cast to OutputStream because otherwise there is an ambiguity over which constructor to use (since PrintStream implements the Appendable, which we don't discuss here).

You can directly obtain the formatted string created by the Formatter by invoking the toString() method. Also, if you simply want a formatted string, such as for a graphical text component, and don't want to send it to an output destination, you can create a Formatter with the no-argument constructor. The Formatter uses internally a StringBuilder (see Chapter 10) , and you can access the string that it creates via the toString() method.

References and Web Resources

Latest update: Nov. 11, 2004

              Tech
Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
HistogramStream
  Demo 3
Filtering Data
  Demo 4
Exercises

           Physics
Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Analysis
Expt. + Analysis
  Demo 3
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.