How can we set dynamic width and precision while formatting string? I know the following code works good, but how can we do it in a formatter way?
int len = 3;
String s = "Andy";
System.out.printf("%1$" + len + "." + len + "s%n", s);
Output:
And
I have tried this one. It didn't throw an error, but prints different value than expected. (It looks so messy, but I've tried to pass the 'len' to width and precision. That's it. :) )
System.out.printf("%2$%1$d.%1$ds%n", len, s);
Output:
%1$d.3s
Is it doable? If so, how can we get same output as the former one?