0

If I want my output string to be at least 5 digits long I can use for example System.out.printf("'%5d'", 10), but can I substitute 5 with some variable which contain the string length? Or what should I use for my output to be as long as X?

Johnny B. Goode
  • 121
  • 1
  • 10

1 Answers1

2

Sure. Just use a variable in the format string:

int length = 50;
System.out.printf("'%"+length+"d'", 10);

Output:

'                                                10'
Malt
  • 28,965
  • 9
  • 65
  • 105