2

I'm practicing my Java skills and I'm trying to left-align everything, however, it's not working as I want it to.

Basically I want something like this:

         Player:    Jackson
Number of tries:    14

And here is what I'm trying:

String format1 = "%-30s %30s%n";

String temp = String.format(format1, "Player:", "Jackson");
System.out.print(temp);
temp = String.format(format1, "Number of tries:", "14");
System.out.print(temp);

However, the words on the left-hand side don't match up, and the words on the right-hand side don't match up.

user207421
  • 305,947
  • 44
  • 307
  • 483
jack55551
  • 21
  • 2
  • Possible duplicate of [How can I left-align strings using String.format()?](http://stackoverflow.com/questions/4893313/how-can-i-left-align-strings-using-string-format) – Barney Apr 23 '17 at 02:25

1 Answers1

1

You need to swap the - in the format.

String format1 = "%30s %-30s%n";

With your code, this produces the following output:

                       Player: Jackson                       
              Number of tries: 14    

You can then improve the formatting from there to get an output closer to your example.

String format1 = "%16s    %-30s%n";

Which gives:

         Player:    Jackson                       
Number of tries:    14  
Obicere
  • 2,999
  • 3
  • 20
  • 31