CSC300: Formatting Output [11/12] |
java.util.Formatter describes formatting conventions.
It's not necessary to use the Formatter
class directly.
The Formatter.format()
method is used by
StdOut.format()
, String.format()
, and similar methods.
01 |
package algs11; import stdlib.*; import java.util.Arrays; public class Hello { public static void main (String[] args) { double d = Math.PI; int i = Integer.MAX_VALUE; double[] lst = { 11, 21, 31 }; StdOut.format ("d=%f, i=%d\nlst=%s%n", d, i, Arrays.toString(lst)); } } |
StdOut.format
takes a variable number of arguments.
%
.
%f
is used for double
s (and float
s)
%d
is used for int
s (and long
s)
%s
is used for String
s
\n
and %n
represent the newline character