THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
LEFT_JUSTIFY, which indicates the flag for left-justification, and
ALTERNATE, which indicates the # flag. Each Formattable class is free
to define what ALTERNATE means.

Additionally, the flag value encodes whether the conversion indicator was lowercase or uppercase using the
FormattableFlags.UPPERCASE constant. You can test whether a flag is set by ANDing the flags
value with the desired constant and seeing if the result is non-zero.


By implementing Formattable a class can provide greater flexibility in how it represents itself textually,
compared to the fixed output of the toString method. For example, it can use a long or short form,
depending on the width and precision that were supplied; or it can adapt itself to the locale used by the
formattersee page 632.


22.1.7. Format Exceptions


If any errors occur while processing a format string with a given set of arguments, an exception that is a
subclass of IllegalFormatException will be thrown. The types of exception you can encounter are



  • DuplicateFormatFlagsException A flag was used more than once.
    FormatFlagsConversionMismatchException A flag was incompatible with the
    conversion.



IllegalFormatCodePointException An integer value was passed that was not a valid
Unicode codepoint.



  • IllegalFormatConversionException The argument was the wrong type for the conversion.

  • IllegalFormatFlagsException The combination of flags was invalid.
    IllegalFormatPrecisionException The precision value was invalid, or the conversion
    does not support a precision value.



IllegalFormatWidthException The width value was invalid, or the conversion does not
support a width value.


MissingFormatArgumentException No argument was supplied in the position expected by a
conversion modifier.



  • MissingFormatWidthException No width was specified when it was required.

  • UnknownFormatConversionException A format's conversion indicator was not known.

  • UnknownFormatFlagsExceptions An unknown flag was given.


22.1.8. The Formatter Class


A Formatter object is always associated with some object that is the destination for the formatted text
produced by the formatter. By default the destination is a StringBuilder. The other destination objects
that can be passed to the various constructors are


An arbitrary Appendable object. This is any object to which characters or CharSequence
objects can be appendedsee page 332 for the methods defined by Appendable.



  • A file, specified either as a File object or a string representing the file name

  • An OutputStream


The constructors for the byte-oriented destinations (files and output streams) either work with the platform's
default encoding or take an additional string parameter that represents the character set encoding name to use.
An additional constructor takes a PrintStream as a destination, but only the default platform encoding is
used.


Formatter supports localization (see Chapter 24) and for each type of destination there is an additional

Free download pdf