I\'m trying to format two arrays in Java to print something like this:
Inventory Number Books Prices
--------------------------
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.
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.