1 of 5

FORMAT() METHOD

2 of 5

Format() Method Definition

The format() method can be used instead of the print() or println() methods to control the way output is displayed. The format() method arguments include a format string and an argument list. The format string contains specifiers that indicate how the corresponding strings in the argument list should be displayed.

3 of 5

Code Sample

System.out.format("%-10s %8s %8s", "Team", "Wins", "Losses\n");

System.out.format("%-10s %8s %8s", "Washington", "10", "5\n");

System.out.format("%-10s %8s %8s", "Seattle", "14", "1\n");

System.out.format("%-10s %8s %8s", "Columbia", "8", "7\n");

System.out.format("%-10s %8s %8s", "Denver", "4", "11\n");

4 of 5

Code Sample

System.out.format("%-10s %8s %8s", "Team", "Wins", "Losses\n");

System.out.format("%-10s %8s %8s", "Washington", "10", "5\n");

System.out.format("%-10s %8s %8s", "Seattle", "14", "1\n");

System.out.format("%-10s %8s %8s", "Columbia", "8", "7\n");

System.out.format("%-10s %8s %8s", "Denver", "4", "11\n");

5 of 5

Code Sample

System.out.format("%-10s %8s %8s", "Team", "Wins", "Losses\n");

%[alignment][width]s

% indicates the start of a specifier

[alignment] skip for right alignment.

Include a minus sign () for left alignment

[width] the number of characters to use for output

s indicates that the corresponding argument is a string