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?
Asked
Active
Viewed 46 times
0

Johnny B. Goode
- 121
- 1
- 10
-
Probably, my bad) – Johnny B. Goode Oct 16 '19 at 12:33
1 Answers
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