Question: I'm trying to format two arrays in Java to print something like this:

```
Inventory Number      Books                          Prices
------------------------------------------------------------------
1                     Intro to Java                  $45.99
2                     Intro to C++                   $89.34
3                     Design Patterns                $100.00
4                     Perl                           $25.00

```

I am using the following code:

```
for(int i = 0; i < 4; i++) {
        System.out.print(i+1);
        System.out.print("                     " + books[i] + " ");
        System.out.print("                 " + "$" + booksPrices[i] + " ");
        System.out.print("\n");
    }

```

But I am getting this poorly formatted result instead:

```
Inventory Number      Books                          Prices
------------------------------------------------------------------
1                     Intro to Java                  $45.99 
2                     Intro to C++                  $89.34 
3                     Design Patterns                  $100.0 
4                     Perl                  $25.0 

```

How would I go about lining all the columns up directly under the headers at the top?

Is there a better way to go about doing this?

Answer: 
You can use

```
System.out.printf(...)

```

for formatting the output. That uses

```
String.format(...)

```

which uses

```
java.util.Formatter

```

you can find the documentation [here](https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html).

To align a simple String, you can use the following:

```
String formatted = String.format("%20s", str)

```

this will prepend

```
20 - str.length

```

blanks before the actual String and will return the padded String.
For example, if your String is "Hello, World!" it will prepend 11 blanks:

```
"           Hello, World!"

```

to align something to the right left, you have to prepend a "-" before the number that indicates the length of the result string.

To safely align everything, you first have to find out what is your longest string.
Without using any libraries, you need to determine the maximum length of each coloumn and append the needed space characters appropriately. For example like this:

```
/**
 * Determines maximum length of all given strings.
 */
public static int maxLength(int padding, String... array) {
    if (array == null) return padding;
    int len = 0;
    for (String s : array) {
        if (s == null) {
            continue;
        }
        len = Math.max(len, s.length());
    }
    return len + padding;
}

/**
 * Unifies array element lengths and appends 3 whitespaces for padding.
 */
public static String[] format(String[] array) {
    if (array == null || array.length == 0) return array;
    int len = maxLength(3, array);
    String[] newArray = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        newArray[i] = array[i];
        while (newArray[i].length() < len) {
            newArray[i] += " ";
        }
    }
    return newArray;
}

```