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 OverflowSystem.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
If you want a minimum of 4 characters, for instance,
System.out.println(String.format("%4d", 5));
// Results in " 5", minimum of 4 characters
Videos
I think you need to use:
String.format("%-6s $%-7.2f Number in Inventory: %-3d", this.getBarcode(), this.getPrice(), this.getInventory());
Or, if you want the space added to the front:
String.format("%-6s $%#7.2f Number in Inventory: %-3d", this.getBarcode(), this.getPrice(), this.getInventory());
Notice the %-7 instead of %-6. The period is counted as a character.
- in %-6.2f means you want passed number to be aligned to left, like:
|123,45|
|123,40|
|123,00|
|12,00 |
|1,00 |
If you want to align your number to right like:
|123,45|
|123,40|
|123,00|
| 12,00|
| 1,00|
then simply remove this -.
You should should also probably set minimal used length to 7 since . also takes some space which means that to produce
| 700.04|
1234567
you need 7 characters.
So try with %-6s $%7.2f Number in Inventory: %-3d
for (int y=1; y<12; y++)
{
System.out.print(String.format("%1$4s", y));
}
It will print Strings of length 4, so if y=1 it will print 3 spaces and 1, " 1", if y is 10 it will print 2 spaces and 10, " 10"
See the javadocs
int i = 0;
while (i < 12) {
System.out.printf("%4d", i);
++i;
}
The 4 is the width. You could also replace printf with format.
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).
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.