System.out.println(String.format("%-20s= %s" , "label", "content" ));
  • Where %s is a placeholder for you string.
  • The '-' makes the result left-justified.
  • 20 is the width of the first string

The output looks like this:

label               = content

As a reference I recommend Javadoc on formatter syntax

Answer from stacker on Stack Overflow
๐ŸŒ
Chron.com
smallbusiness.chron.com โ€บ spacing-output-java-32722.html
Spacing of Output in Java | Small Business - Chron.com
February 1, 2019 - If the values of "i," "j" and "k" are 25, 6 and 31, respectively, the program outputs "25 6 31." This method's primary drawback is the lack of consideration for the number of characters in each integer. If you print three rows of evenly spaced columns, the second column is not going to be the same width as the first two. If the next row held the values 6, 8, and 2, the columns would be entirely misaligned. Java's Formatter class allows you to format data before it is output.
๐ŸŒ
Mathbits
mathbits.com โ€บ JavaBitsNotebook โ€บ DataBasics โ€บ FormattingOutput.html
Formatting Output - JavaBitsNotebook.com
Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms of Use | JavaMathBits.com Formatting Output ยท Formatting options are of particular benefit when creating charts or tables of information. There are a variety of ways to format data in Java
๐ŸŒ
Home and Learn
homeandlearn.co.uk โ€บ java โ€บ java_formatted_strings.html
Java For Complete Beginners - formatted strings
Same as above but the numbers occupy 10 places, with spaces to the left as padding. Finally, here's the table again from the start of this formatting section: And here's the code for the above formatted output: Have a play around with formatting, and see how you get on. If you get error messages you may have gotten your "s" formatting confused with your "d" formatting! In the next section, we'll move on and tackle Java ...
๐ŸŒ
DEV Community
dev.to โ€บ realedwintorres โ€บ how-to-formatting-java-output-with-printf-1bik
How-To: Formatting Java Output With printf() - DEV Community
November 7, 2021 - The third field format .2f indicates a floating-point field that has a total width of 10, right-justified. It uses 2 spaces for the decimal places and 1 space for the decimal point. This formats the double variable weight.
Find elsewhere
๐ŸŒ
Java2s
java2s.com โ€บ Tutorials โ€บ Java โ€บ Java_Format โ€บ 0090__Java_Format_General.htm
Java Format - Java printf format
The general syntax for a format ... width specifies the minimum number of characters in the output. If the length of the string representation is less than the width value, the result will be padded with spaces......
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ space-format-specifiers-in-java
Space format specifiers in Java
July 30, 2019 - The following is an example displaying different forms of space format specifiers โˆ’ ... import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f); f = new Formatter(); f.format(".2f", -7.598); System.out.println(f); f = new Formatter(); f.format("% 12.3f", 90.87787); System.out.println(f); } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ formatting output with printf() in java
Formatting Output with printf() in Java | Baeldung
January 8, 2024 - As we can see, the formatting in the US is different than in Italy: ... Here we have one-space padding from the beginning of the number to support the predefined width. To have our output in scientific notation, we just use the e conversion-character:
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 7 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Formatter.html
Formatter (Java Platform SE 7 )
If the '#' flag is given, then ... to be written to the output. If the length of the converted value is less than the width then the output will be padded by spaces ('\u0020') until the total number of characters equals width....
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Formatter.html
Formatter (Java Platform SE 8 )
April 21, 2026 - If the '#' flag is given, then ... to be written to the output. If the length of the converted value is less than the width then the output will be padded by spaces ('\u0020') until the total number of characters equals width....
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 19594164 โ€บ maintaining-proper-spacing-in-java
formatting - Maintaining proper spacing in Java - Stack Overflow
October 25, 2013 - If you want to right justify, you can either - write the output in a file as a CSV and open it in an excel like program - create a utility class that will make any input string a constant length: public static String fixedCharCount( String input, int length ) { int spacesToAdd = length - input.lengh(); StringBuffer buff = new StringBuffer(input); for( int i=0; i<spacesToAdd; i++) { buff.append(" "); } return buff.toString(); }
Top answer
1 of 4
18

I'm also going with the "format" suggestion, but mine is a little different.

In your program, you're relying on your card's toString. So make that formatted in a fixed length, and then you can use them anywhere and they will take up the same space.

public String toString() {
    return String.format( "%5s of %-8s", rank, suit );
}

When you print this out, you'll have all your cards aligned on the "of" part, which I think is what you were going for in your first output column.

The "%5s" part right-aligns the rank in a field 5 characters wide, and the "%-8s" part left-aligns the suit in a field 8 characters wide (which means there are additional spaces to the right if the suit is shorter than 8 characters).

2 of 4
14

You use something similar to what you are probably using right now. It looks like you are printing out using System.out.print(); right? If so, you can use System.out.printf(); along with %#. Let me explain.

It looks like you want to print out something that looks like this.

Column 1     Column 2     Column 3
12           23           1234

Well, the easy way to solve for that would be like this.

  int c1 = 12;
  int c2 = 23;
  int c3 = 1234;

  System.out.printf("%-22s%-22s%-22s\n","Column 1","Column 2","Column 3");
  System.out.printf("%-22d%-22d%-22d\n",c1,c2,c3);

Let's explain what is going on here, there's a lot of stuff happening. First, whenever you see a % sign in System.out.printf(), it means a special (formatting in this case) action is about to follow.

Now, let's look at the parameters of the method. The way System.out.printf() works is by specifying the format of the output in the first parameter. Then, the actual outputs are the following parameters. Do you see how it starts by specifying the format with "%-22s%-22s%-22s\n"? Then, it actually outputs them, separated by commas? That's IMO the easiest way to format.

Lastly, let's look some more at that special action thing I mentioned earlier. You see how there are letters and numbers and - after the %? Those all serve purposes too. --HERE-- is a good place to learn more but I'll give you the basic rundown.

First, the negative sign specifies which side will receive padding, padding is the space between the columns that makes them look pretty. Since it is negative, it will be on the right side, (FYI, if there was no sign there, padding would be on the left) so it will print out your output, then add spaces to the right... but how many spaces?

That's where that number comes in. The 17's you see are how many spaces there will be subtracted from the length of the output. Long story short, it will plainly and simply make sure each output starts and ends at the same place. The only catch is, make sure that the number, 22, is longer than the maximum possible String. I see the longest one possible would be Queen of Diamonds so anything bigger than 19 should do it, I chose 22 cuz it looked better IMO.

Next is the letter, that letter, as you can see, changes between the 2 output statements. In this case, one says s, the other says d. Easy way to solve this is, if your output is a String, use s, if it's and int (I don't think you will need it, I just had to for my example cuz mine were ints), use d.

๐ŸŒ
HackerRank
hackerrank.com โ€บ challenges โ€บ java-output-formatting โ€บ forum
Java Output Formatting Discussions | Java | HackerRank
public static void main(String[] args) { Scanner sc=new Scanner(System.in); StringBuilder s= new StringBuilder(); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x=sc.nextInt(); for(int j=0;j<15-s1.length();j++){ s.append(" "); } System.out.println(s1+s+String.format("d",x)); s.setLength(0); } System.out.println("================================"); }
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to pad a string in java?
How to pad a String in Java? - Mkyong.com
March 11, 2020 - This article shows you how to use the JDK1.5 String.format() and Apache Common Lang to left or right pad a String in Java. By default, the String.format() fills extra with spaces \u0020.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ java-output-formatting
Java Output Formatting - Javatpoint
Java Output Formatting with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ set-width-and-precision-to-format-output-in-java
Set width and precision to format output in Java
July 30, 2019 - Set width in output as shown below. Here,  sets a 10 character field System.out.printf("d1 = .2f \nd2 = %8g", d1, d2); Above, the value after the decimal is for decimal places i.e. 5.3 is