Pepper Pepper - 7 months ago 9
Java Question

How do I format the following String in Java

So, I have the following piece of code has an example:

StringBuilder sb = new StringBuilder (" ");

for (int i = 0; i < 20; i++)
sb.append(" " + i);

sb.append("\n");

for (int i = 0; i < 20; i++) {
sb.append(i + " ");

for (int j = 0; j < 20; j++)
if (j % 2 == 0)
sb.append("A ");

else
sb.append("B ");

sb.append("\n");

}


As I go further printing the A's and B's they won't stay under their respective number and the whole text starts to look bad. I was told I could use the Format class to solve this problem but I see no way to do it. I was wondering if you could help me! Thank you.

Riz Riz
Answer

Use tabs, like

sb.append("\tA")
sb.append("\tB")

Do the same for the headings , to keep them in line