Align predefined text to left and display string aligned to the right. Java

2k views Asked by At

In Java, how do I use print or printf to have A line of text, that has the first output as "Character Name:" (which is aligned to the left by default) and then show a string value (char1) to be aligned to the right of the screen, or aligned to the right of a column? ie:

   Character Name:                      char1

   Character Value:                 charValue

ps. I am using eclipse if that helps.

1

There are 1 answers

2
maccartm On
System.out.format("Character Name: %25s", x);
System.out.format("Character Value: %24s", y);

You will have to mess around with the formatting to decide how many spaces you'll have to include in between; it will depend on the length of the left justified word. But using a format %s will right justify by default, or left justify if you use %-s. The number between % & s is the amount of spaces in between.