How-To: Formatting Java Output With printf()
Tags: java, codenewbie, tutorial, 100daysofcode
Published: 2021-11-07 19:52:02

---
title: How-To: Formatting Java Output With printf()
published: true
description: How-To: Formatting Java Output With printf()
tags: Java, codenewbie, tutorial, 100daysofcode
//cover_image: https://direct_url_to_image.jpg
---

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**

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

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

  }
}
```

**Output**

```bash
$  javac Example.java  # compile
$  java Example  # run
fishing
$
```

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 `"%s\n"` 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**

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

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

  }
}
```

**Output**

```bash
$  javac Example.java
$  java Example
***fishing***
$  
```

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**

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

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

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

  }
}
```

**Output**

```bash
$  javac Example.java
$  java Example
red, orange, yellow
$
```

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**

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

    String c1 = "red";

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

  }
}
```

**Output**

```bash
$  javac Example.java
$  java Example
  red
$
```

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**

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

    String c1 = "red";

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

  }
}
```

**Output**

```bash
$  javac Example.java
$  java Example
red  !
$
```

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**

```bash
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);

  }
}
```

**Output**

```bash
$  java Example
Item   |apples|
Qty    | 100|
Weight |     36.44|
$
```

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`](https://twitter.com/realEdwinTorres) for more programming tips. 😀