Java Reference
In-Depth Information
Formatting Characters and Strings
The following code fragment outputs characters and their code values:
int count = 0;
for(int ch = 'a' ; ch<= 'z' ; ch++) {
System.out.printf(" %1$4c%1$4x", ch);
if(++count%6 == 0) {
System.out.printf("%n");
}
}
TryFormattedOutput.java
Executing this produces the following output:
a 61 b 62 c 63 d 64 e 65 f 66
g 67 h 68 i 69 j 6a k 6b l 6c
m 6d n 6e o 6f p 70 q 71 r 72
s 73 t 74 u 75 v 76 w 77 x 78
y 79 z 7a
First the format specification
%1$4c
is applied to the first and only argument following the format string.
This outputs the value of
ch
as a character because of the
'c'
conversion specification and in a field width
of 4. The second specification is
%1$4x
, which outputs the same argument — because of the
1$
— as hexa-
decimal because the conversion is
'x'
and in a field width of 4.
You could write the output statement in the loop as:
System.out.printf(" %1$4c%<4x", ch);
The second format specifier is
%<4x
, which outputs the same argument as the preceding format specifier
because of the
'<'
following the
%
sign.
Because a
%
sign always indicates the start of a format specifier, you must use
"%%"
in the format string
when you want to output a
%
character. For example:
int percentage = 75;
System.out.printf("\n%1$d%%", percentage);
The format specifier
%1$d
outputs the value of percentage as a decimal value. The
%%
that follows in the
format string displays a percent sign, so the output is:
75%
You use the
%s
specifier to output a string. Here's an example that outputs the same string twice:
String str = "The quick brown fox.";
System.out.printf("%nThe string is:%n%s%n%1$25s", str);