\t
inserts a TAB character, not spaces. Knowing this, you should not use tabs to format the output of your data.
Since this looks for displaying data to user, it would be better using String#format
which allows providing spaces to both left and right sides for your text.
Here's an example using %-15s
:
//splitted in 2 lines for better understanding
String outputText = String.format("Of what race do you belong? %-15s %-15s %-15s %-15s %-15s",
"[H]uman", "[E]lf", "[D]warf", "[O]rc", "[G]nome");
System.out.println(outputText);
Where:
%s
means that it will add a String
to the output.
15
is the number of spaces to locate the string
-
indicates it should be left aligned.
Output of code above:
Of what race do you belong? [H]uman [E]lf [D]warf [O]rc [G]nome
You can shorten the length of the spaces by just modifying the -15
.
More info about how to proper format your output text:
Note that using System.out.printf
uses Formatter
behind the scene as well.