1

Is it possible to set shift nor by default but programmatically? I mean if I have a code

System.out.format("%-d%d", shift, value);

it returns java.util.MissingFormatWidthException. Instead of error I want to set the shift dynamically.

How can I do that?

barbara
  • 2,931
  • 5
  • 27
  • 56

1 Answers1

1

If I understand you correctly, you can create your format String on the fly, but it has to be a two-step process:

String formatString = "%-" + shift + "d";
System.out.format(formatString, value);

or

String formatString = String.format("%%-%sd", shift); // %% for the single %
System.out.format(formatString, value);

or as Christian notes

System.out.format("%-" + shift + "d", value);
Hovercraft Full Of Eels
  • 280,125
  • 25
  • 247
  • 360