braju.com
Java printf

com.braju.format
Class Format

java.lang.Object
  |
  +--com.braju.format.BasicFormatString
        |
        +--com.braju.format.Format

public class Format
extends com.braju.format.BasicFormatString

This class provides static C-style methods like sprintf(), printf() and fprintf() with variable number of parameters.

Format specification

This is the format of one conversion specification.

.    __%__ _________ __ _________ __ ________________ __type__
 .         |         |  |         |  |                |
 .         |__flags__|  |__width__|  |__.__precision__|

or

% flag* [width] [.precision] type

Field description

flags Justification of output and printing of signs, blanks, decimal points, octal, and hexadecimal prefixes, and the semantics for wchar_t precision unit.

width Minimum number of characters (bytes) output.

precision Maximum number of characters (bytes) printed for all or part of the output field, or minimum number of digits printed for integer values.

Flag Brief description
- (minus sign) Left adjusted, otherwise right adjusted.
+ (plus sign) Prefix the output value with a sign (+ or -) if the output value is of a signed type. Default is that sign appears only for negative values (-). 
' ' (blank) A nonnegative value will have a space prepended. The + flag overrides the blank flag if both appear, and a positive value will be output with a sign. 
# (pound) Alternative form. When used with the o, x, X, b or B formats, the # flag prefixes any output value with 0, 0x, 0X, 0b or 0B, respectively. When used with the f, e, or E formats, the # flag forces the output value to contain a decimal point in all cases. When used with the c format, the # flag forces the output value to be in UTF-encoding, e.g. A. 
0 (zero) When used with the b, B, d, i, u, o, x, X, e, E, f, g, or G formats, the 0 flag causes leading 0's to pad the output to the field width. The 0 flag is ignored if the - flag is specified. 
 
Width Brief description
* (asterix) The value for the precision will be obtained from the parameter list. If the value is negative the output will be left-justified.
n (an integer) A nonnegative integer for the width; e.g. 8.
 
Precision Brief description
* (asterix) The value for the width will be obtained from the parameter list.
n (an integer) A nonnegative integer for the precision; e.g. 3.
 
Conversion 
type
Corresponding argument 
will be printed as
l,L1 a logical value, i.e. a boolean values; e.g. true, FALSE. Also support for C-style booleans, i.e. 0 or 1 (<>0). 
c a character.
d, i a decimal integer.
u an unsigned integer. This flag is applicable to the byte, the short and the int data types only.
b1 a binary integer.
x,X a hexadecimal integer.
o an octal integer.
e,E a scientific floating point number; e.g. 1.23E-03.
f a floating point number; e.g. 0.00123.
g,G %e or %E is used if the exponent is less than -4 or greater than or equal to the precision; otherwise %f is used. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
s a string. All characters in String is printed or as many as precision specifies.
% There is no corresponding argument, i.e. To output "%" one have to put "%%" in the format string.

Not an ANSI-C standard.

Example:

 Format.printf("Please, try to support %3.2f%% %s!\n", new Parameters(100).add("Java"));
 
gives
 Please, try to support 100.00% Java!
 
You can also do like this
 Parameters p = new Parameters();
 Format.printf("Please, try to support %.2f%% %s!\n", p.add(100).add("Java"));
 Format.printf("Or, at least %.1f%%.\n", p.add(99.945));
 
which gives
 Please, try to support 100.00% Java!
 Or, at least too 99.9%.
 

For more examples see the official Java printf site: www.braju.com.

See Also:
Parameters, ParametersAutoClear

Method Summary
static void fprintf(java.io.OutputStream out, java.lang.String fmt)
          The format-string is passed to the stream.
static void fprintf(java.io.OutputStream out, java.lang.String fmt, java.lang.Object[] params)
          The parameters in Object[] are converted according to the format-string and then passed to the stream.
static void fprintf(java.io.OutputStream out, java.lang.String fmt, Parameters params)
          The parameters are converted according to the format-string and then passed to the stream.
static void fprintf(java.io.OutputStream out, java.lang.String fmt, java.util.Vector params)
          The parameters in the vector are converted according to the format-string and then passed to the stream.
static void fprintf(java.io.Writer out, java.lang.String fmt)
          The format-string is passed to the writer [not supported in the Java 1.0.2 version].
static void fprintf(java.io.Writer out, java.lang.String fmt, java.lang.Object[] params)
          The parameters in Object [] are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version].
static void fprintf(java.io.Writer out, java.lang.String fmt, Parameters params)
          The parameters are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version].
static void fprintf(java.io.Writer out, java.lang.String fmt, java.util.Vector parameters)
          The parameters in the vector are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version].
static boolean getModifyLineSeparators()
          Return true if the Format-class replace all occurens of '\n' with the line separator of the local system, i.e. line.separator.
static java.lang.String getVersion()
          Gets the version as a string.
static void main(java.lang.String[] args)
          This is the command line version of Java printf.
static void printf(java.lang.String fmt)
          The format-string is printed to the standard output.
static void printf(java.lang.String fmt, java.lang.Object[] params)
          The parameters in Object[] are converted according to the format-string and printed to the standard output.
static void printf(java.lang.String fmt, Parameters params)
          The parameters are converted according to the format-string and printed to the standard output.
static void printf(java.lang.String fmt, java.util.Vector parameters)
          The parameters in the vector are converted according to the format-string and printed to the standard output.
static void setModifyLineSeparators(boolean status)
          By setting the modifyLineSeparators property the Format-class will replace all occurens of '\n' with the line separator of the local system, i.e.
static java.lang.String sprintf(java.lang.String fmt, java.lang.Object[] params)
          The parameters in Object[] are converted according to the format-string and then returned as a string.
static java.lang.String sprintf(java.lang.String fmt, Parameters parameters)
          The parameters are converted according to the format-string and then returned as a string.
static java.lang.String sprintf(java.lang.String fmt, java.util.Vector parameters)
          The parameters in the vector are converted according to the format-string and then returned as a string.
static void version()
          Print, on standard output, information about this class.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fprintf

public static void fprintf(java.io.OutputStream out,
                           java.lang.String fmt)
                    throws java.io.IOException,
                           ParseErrorException
The format-string is passed to the stream. The format-string can not contain any conversion specifications, if it does the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.OutputStream out,
                           java.lang.String fmt,
                           java.lang.Object[] params)
                    throws java.io.IOException,
                           ParseErrorException
The parameters in Object[] are converted according to the format-string and then passed to the stream. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.OutputStream out,
                           java.lang.String fmt,
                           Parameters params)
                    throws java.io.IOException,
                           ParseErrorException
The parameters are converted according to the format-string and then passed to the stream. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.OutputStream out,
                           java.lang.String fmt,
                           java.util.Vector params)
                    throws java.io.IOException,
                           ParseErrorException
The parameters in the vector are converted according to the format-string and then passed to the stream. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.Writer out,
                           java.lang.String fmt)
                    throws java.io.IOException,
                           ParseErrorException
The format-string is passed to the writer [not supported in the Java 1.0.2 version]. The format-string can not contain any conversion specifications, if it does the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.Writer out,
                           java.lang.String fmt,
                           java.lang.Object[] params)
                    throws java.io.IOException,
                           ParseErrorException
The parameters in Object [] are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version]. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.Writer out,
                           java.lang.String fmt,
                           Parameters params)
                    throws java.io.IOException,
                           ParseErrorException
The parameters are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version]. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

fprintf

public static void fprintf(java.io.Writer out,
                           java.lang.String fmt,
                           java.util.Vector parameters)
                    throws java.io.IOException,
                           ParseErrorException
The parameters in the vector are converted according to the format-string and then passed to the writer [not supported in the Java 1.0.2 version]. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

java.io.IOException
ParseErrorException

getModifyLineSeparators

public static boolean getModifyLineSeparators()
Return true if the Format-class replace all occurens of '\n' with the line separator of the local system, i.e. line.separator. Otherwise, false is returned.


getVersion

public static java.lang.String getVersion()
Gets the version as a string.

Returns:
the version as a String.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
This is the command line version of Java printf.

Usage:

  java com.braju.format.Format [-help|-version|-printf fmtstr [param]*]

Options:
-helpPrint this message.
-printfUsing the next argument (fmtstr) as the format string and one or more optional parameters (param) as values to be formatted by the printf method.
-versionPrint version information.

Example:

  java com.braju.format.Format -printf "Yeah, %d%% %s." 100 Java

prints

  Yeah, 100% Java.

java.lang.Exception
See Also:
www.braju.com

printf

public static void printf(java.lang.String fmt)
                   throws ParseErrorException
The format-string is printed to the standard output. The format-string can not contain any conversion specifications, if it does the behavior is unknown.

ParseErrorException

printf

public static void printf(java.lang.String fmt,
                          java.lang.Object[] params)
                   throws ParseErrorException
The parameters in Object[] are converted according to the format-string and printed to the standard output. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

ParseErrorException

printf

public static void printf(java.lang.String fmt,
                          Parameters params)
                   throws ParseErrorException
The parameters are converted according to the format-string and printed to the standard output. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

ParseErrorException

printf

public static void printf(java.lang.String fmt,
                          java.util.Vector parameters)
                   throws ParseErrorException
The parameters in the vector are converted according to the format-string and printed to the standard output. The number of conversion specifications must match the number of parameters, otherwise the behavior is unknown.

ParseErrorException

setModifyLineSeparators

public static void setModifyLineSeparators(boolean status)
By setting the modifyLineSeparators property the Format-class will replace all occurens of '\n' with the line separator of the local system, i.e. line.separator. By default is this property activated. In applets, where it is impossible to get the line.separator due to security reasons nothing is replaced. If the line separator is '\r\n' and the output is '\n\r\n' the result is '\r\n\r\n', i.e. allready correct line separators are not modified.


sprintf

public static java.lang.String sprintf(java.lang.String fmt,
                                       java.lang.Object[] params)
                                throws ParseErrorException
The parameters in Object[] are converted according to the format-string and then returned as a string. The number of conversion specifications must match the number of parameters. If not, null is returned.

Returns:
The converted string if possible, otherwise null is returned.
ParseErrorException

sprintf

public static java.lang.String sprintf(java.lang.String fmt,
                                       Parameters parameters)
                                throws ParseErrorException
The parameters are converted according to the format-string and then returned as a string. The number of conversion specifications must match the number of parameters. If not, null is returned.

Returns:
The converted string if possible, otherwise null is returned.
ParseErrorException

sprintf

public static java.lang.String sprintf(java.lang.String fmt,
                                       java.util.Vector parameters)
                                throws ParseErrorException
The parameters in the vector are converted according to the format-string and then returned as a string. The number of conversion specifications must match the number of parameters. If not, null is returned.

Returns:
The converted string if possible, otherwise null is returned.
ParseErrorException

version

public static void version()
Print, on standard output, information about this class.


braju.com
Java printf

Copyright 1997-2003, Henrik Bengtsson.