How-To: Formatting Java Output With printf()

Posted on

The Java System.out.printf() method formats and outputs a string. You specify the data and the format of the output. The examples below demonstrate different ways to use printf() to format output.

To output a single string value, use the %s format:

Source code

public class Example {
  public static void main(String[] args) {

    String hobby = "fishing";
    System.out.printf("%sn", hobby);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  javac Example.java  # compile
$  java Example  # run
fishing
$
Enter fullscreen mode

Exit fullscreen mode

The printf() statement has parameters. The first parameter is the format string. The remaining parameters are the data values.

A format string has field formats and any characters to output as-is. A field format starts with a percent symbol (%), an optional width, and the type of the data value. In the program above, the format string "%sn" indicates:

  • One string field: %s
  • One newline character: n

The second parameter of the printf() statement is the value to output, namely the variable hobby.

You can customize the format string any way you like by adding characters to it. This example puts three asterisk (*) characters before and after the string value:

Source code

public class Example {
  public static void main(String[] args) {

    String hobby = "fishing";
    System.out.printf("***%s***n", hobby);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  javac Example.java
$  java Example
***fishing***
$  
Enter fullscreen mode

Exit fullscreen mode

The printf() statement can format multiple values. Just add the values after the format string. Each value corresponds to each field format, in order. This example outputs three colors, separated by a comma and a space:

Source code

public class Example {
  public static void main(String[] args) {

    String c1 = "red";
    String c2 = "orange";
    String c3 = "yellow";

    System.out.printf("%s, %s, %sn", c1, c2, c3);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  javac Example.java
$  java Example
red, orange, yellow
$
Enter fullscreen mode

Exit fullscreen mode

The format string specifies three %s formats. The three string variables follow: c1, c2, and c3.

You can specify a width in the %s string format field. This will output the string value, right-justified, in a field of that width. For example, %5s specifies a string field of width 5 and right justified:

Source code

public class Example {
  public static void main(String[] args) {

    String c1 = "red";

    System.out.printf("%5sn", c1);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  javac Example.java
$  java Example
  red
$
Enter fullscreen mode

Exit fullscreen mode

Since the field is right-justified, two spaces appear before red. To make the field left-justified, include a hyphen (-) in the format field, just after the percent (%) symbol:

Source code

public class Example {
  public static void main(String[] args) {

    String c1 = "red";

    System.out.printf("%-5s!n", c1);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  javac Example.java
$  java Example
red  !
$
Enter fullscreen mode

Exit fullscreen mode

Now the field is left-justified. The ! proves that there are two spaces after red.

You can format other Java data types too. To format an integer type, use %d. To format a floating-point value, use %f. With floating-point formats, you can specify the field width and the number of decimal places. Here is an example:

Source code

public class Example {
  public static void main(String[] args) {

    String fruit = "apples";
    int  quantity = 100;
    double weight = 36.443;

    System.out.printf("Item   |%s|nQty    |%4d|nWeight |%10.2f|n", fruit, quantity, weight);

  }
}
Enter fullscreen mode

Exit fullscreen mode

Output

$  java Example
Item   |apples|
Qty    | 100|
Weight |     36.44|
$
Enter fullscreen mode

Exit fullscreen mode

The program above has pipe (|) characters to show the columns.

The first field format %s just outputs the string variable fruit.

The second field format %4d indicates an integer field that has a width of 4, right-justified. It outputs the integer variable quantity.

The third field format %10.2f indicates a floating-point field that has a total width of 10, right-justified. It uses 2 spaces for the decimal places and 1 space for the decimal point. This formats the double variable weight.

The Java printf() statement can output data in many ways. You just need to supply the data values, field formats, and output characters.

Follow me on Twitter @realEdwinTorres for more programming tips.

Leave a Reply

Your email address will not be published.