| 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 doubles (and floats)
%d is used for ints (and longs)
%s is used for Strings
\n and %n represent the newline character