0

I want to add padding to left and right of my string and print something like:

|          WELCOME TO THE GAME          |

I am trying this:

System.out.printf("|%15s%20s|%n",header,"");

and the output is:

|WELCOME TO THE GAME                    |

I have 2 questions here:

  1. Any better way to do padding both left and right of my keyword without actually using an empty string?
  2. What would be the right formatting to get the desired output?
kukroid
  • 420
  • 6
  • 15
  • 1
    Do you want a fixed padding (always 10 spaces to the left and right) or a fixed width (the whole string is always 40 characters long)? – Sweeper Apr 13 '21 at 03:43
  • If the latter, then a duplicate of [this](https://stackoverflow.com/questions/8154366/how-to-center-a-string-using-string-format). – Sweeper Apr 13 '21 at 03:46
  • 1
    Does this answer your question? [How to center a string using String.format?](https://stackoverflow.com/questions/8154366/how-to-center-a-string-using-string-format) – the Hutt Apr 13 '21 at 06:23

1 Answers1

0

change it to 30, 10

System.out.printf("|%30s%10s|%n",header,"");

it's output

|           WELCOME TO THE GAME          |
sanjeevRm
  • 1,541
  • 2
  • 13
  • 24