I want to create something like this, where each line is surrounded by the “pipe” char.
| First Line |
| 100 200 |
| 1000 2000 |
- In the first line, the right padding is 1 space.
- In the second line, the right padding is 4 spaces.
- In the third line is 2 spaces.
I’m trying to do it via printf + formating (and not explicitly calculating a padding number) but I’m having some trouble with the formating syntax. Here’s my current code:
System.out.printf("| FIRST LINE" + "%50s\n", "|");
System.out.printf("| 100 200" + "%50s", "|");
System.out.printf("| 1000 2000" + "%50s", "|");
I’m trying to indicate that the maximum per line is 50 chars, being the first character in the line a “pipe” and the last character in the line another “pipe”).
The problem is that the 50 spaces are getting placed without taking in consideration the characters already used in the left part (i.e. “| FIRST LINE”). The code above is similar to:
System.out.format("%s %50s\n", "| FIRST LINE", "|");
So, how can I define the output format such that both strings are taken in consideration for the width?
Thanks in advance.
Try
Formatter.e.g.
Output: