Lecture 12B
Cloning
File Read by Applet
RandomIO,Append
DecimalFormat
DecimalFormat-IBM
Precision Class-KI
Format - Core Java
Output to String

Console Demo
Exercises

Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture12B : Core Java's Format
Core Java's Format Class

Core Java by C. S. Horstmann & G. Cornell also provides a class intended for output formatting.

The class Format provides the Format.print(..) static methods (see below) to output a single number or string with formatting similar to the printf function in C.

Below we show part of the Format.java source code and the output to the console of the test stub.

Note that the class uses the older PrintStream class instead of PrintWriter, which is now preferred for text output.

As shown below, the print methods were duplicated with StringWriter streams. In the next page we show how to use these methods to send formatted outputs to a string.

Format.java

/**
 * A class for formatting numbers that follows printf
 * conventions.
 * Also implements C-like atoi and atof functions
 * @version 1.01 15 Feb 1996
 * @author Cay Horstmann
 */

package corejava;

import java.io.*;

public class Format

{ /**
  * Formats the number following printf conventions.
  * Main limitation: Can only handle one format parameter
  * at a time
  * Use multiple Format objects to format more than one number
  * @param s the format string following printf conventions
  * The string has a prefix, a format code and a suffix.
  * The prefix and suffix
  * become part of the formatted output. The format code
  * directs the
  * formatting of the (single) parameter to be formatted.
  * The code has the
  * following structure
  * <ul>
  * <li> a % (required)
  * <li> a modifier (optional)
  * <dl>
  * <dt> + <dd> forces display of + for positive numbers
  * <dt> 0 <dd> show leading zeroes
  * <dt> - <dd> align left in the field
  * <dt> space <dd> prepend a space in front of positive numbers
  * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal
  * or hexadecimal numbers. Don't suppress trailing zeroes in
  * general floating point format.
  * </dl>
  * <li> an integer denoting field width (optional)
  * <li> a period followed by an integer denoting
  * precision (optional)
  * <li> a format descriptor (required)
  * <dl>
  * <dt>f <dd> floating point number in fixed format
  * <dt>e, E <dd> floating point number in exponential notation
  * (scientific format). The E format results in an uppercase
  * E for the exponent (1.14130E+003), the e format in a
  * lowercase e.
  * <dt>g, G <dd> floating point number in general format
  * (fixed format for small numbers, exponential format for
  * large numbers). Trailing zeroes are suppressed. The G
  * format results in an uppercase E for the exponent (if any),
  * the g format in a lowercase e.
  * <dt>d, i <dd> integer in decimal
  * <dt>x <dd> integer in hexadecimal
  * <dt>o <dd> integer in octal
  * <dt>s <dd> string
  * <dt>c <dd> character
  * </dl>
  * </ul>
  * @exception IllegalArgumentException if bad format
  */

   public Format(String s)
   { 

[..See the source code for details of
how formatting is performed..]


  /**
  * prints a formatted number following printf conventions
  * @param s a PrintStream
  * @param fmt the format string
  * @param x the double to print
  */
 
   public static void print(java.io.PrintStream s,
       String fmt, double x)

   {  s.print(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a PrintStream
  * @param fmt the format string
  * @param x the long to print
  */
  public static void print(java.io.PrintStream s,
      String fmt, long x)

   {  s.print(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a PrintStream
  * @param fmt the format string
  * @param x the character to
  */
 
   public static void print(java.io.PrintStream s,
      String fmt, char x)

   {  s.print(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a PrintStream, fmt the format string
  * @param x a string that represents the digits to print
  */
 
   public static void print(java.io.PrintStream s,
     String fmt, String x)

   {  s.print(new Format(fmt).form(x));
   }

// The following print() methods with StringWriter streams
// was added to allow for formatted output to a String object.

  /**
  * prints a formatted number following printf conventions
  * @param s a StringWriter
  * @param fmt the format string
  * @param x the double to print
  */

   public static void print(java.io.StringWriter s,
                            String fmt, double x)

   {  s.write(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a StringWriter
  * @param fmt the format string
  * @param x the long to print
  */
  public static void print(java.io.StringWriter s,
                           String fmt, long x)

   {  s.write(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a StringWriter
  * @param fmt the format string
  * @param x the character to
  */

   public static void print(java.io.StringWriter s,
                            String fmt, char x)

   {  s.write(new Format(fmt).form(x));
   }

  /**
  * prints a formatted number following printf conventions
  * @param s a StringWriter, fmt the format string
  * @param x a string that represents the digits to print
  */

   public static void print(java.io.StringWriter s,
                            String fmt, String x)

   {  s.write(new Format(fmt).form(x));
   }


 
 
[....more code....]


   /**
   * a test stub for the format class
   */
 
   public static void main(String[] a)
   {  double x = 1.23456789012;
      double y = 123;
      double z = 1.2345e30;
      double w = 1.02;
      double u = 1.234e-5;
      int d = 0xCAFE;
      Format.print(System.out, "x = |%f|\n", x);
      Format.print(System.out, "u = |%20f|\n", u);
      Format.print(System.out, "x = |% .5f|\n", x);
      Format.print(System.out, "w = |%20.5f|\n", w);
      Format.print(System.out, "x = |%020.5f|\n", x);
      Format.print(System.out, "x = |%+20.5f|\n", x);
      Format.print(System.out, "x = |%+020.5f|\n", x);
      Format.print(System.out, "x = |% 020.5f|\n", x);
      Format.print(System.out, "y = |%#+20.5f|\n", y);
      Format.print(System.out, "y = |%-+20.5f|\n", y);
      Format.print(System.out, "z = |%20.5f|\n", z);
 
      Format.print(System.out, "x = |%e|\n", x);
      Format.print(System.out, "u = |%20e|\n", u);
      Format.print(System.out, "x = |% .5e|\n", x);
      Format.print(System.out, "w = |%20.5e|\n", w);
      Format.print(System.out, "x = |%020.5e|\n", x);
      Format.print(System.out, "x = |%+20.5e|\n", x);
      Format.print(System.out, "x = |%+020.5e|\n", x);
      Format.print(System.out, "x = |% 020.5e|\n", x);
      Format.print(System.out, "y = |%#+20.5e|\n", y);
      Format.print(System.out, "y = |%-+20.5e|\n", y);
 
      Format.print(System.out, "x = |%g|\n", x);
      Format.print(System.out, "z = |%g|\n", z);
      Format.print(System.out, "w = |%g|\n", w);
      Format.print(System.out, "u = |%g|\n", u);
      Format.print(System.out, "y = |%.2g|\n", y);
      Format.print(System.out, "y = |%#.2g|\n", y);

      Format.print(System.out, "d = |%d|\n", d);
      Format.print(System.out, "d = |%20d|\n", d);
      Format.print(System.out, "d = |%020d|\n", d);
      Format.print(System.out, "d = |%+20d|\n", d);
      Format.print(System.out, "d = |% 020d|\n", d);
      Format.print(System.out, "d = |%-20d|\n", d);
      Format.print(System.out, "d = |%20.8d|\n", d);
      Format.print(System.out, "d = |%x|\n", d);
      Format.print(System.out, "d = |%20X|\n", d);
      Format.print(System.out, "d = |%#20x|\n", d);
      Format.print(System.out, "d = |%020X|\n", d);
      Format.print(System.out, "d = |%20.8x|\n", d);
      Format.print(System.out, "d = |%o|\n", d);
      Format.print(System.out, "d = |%020o|\n", d);
      Format.print(System.out, "d = |%#20o|\n", d);
      Format.print(System.out, "d = |%#020o|\n", d);
      Format.print(System.out, "d = |%20.12o|\n", d);
 
      Format.print(System.out, "s = |%-20s|\n", "Hello");
      Format.print(System.out, "s = |%-20c|\n", '!');
   }

}
 

The Format.java includes a test stub and when gives the following output:

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey