CSC300 / CSC402: Formatting Output [10/11] Previous pageContentsNext page

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
02
03
04
05
06
07
08
09
10
11
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.

Previous pageContentsNext page