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
  • 3,111
  • 6
  • 30
  • 58

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
  • 283,665
  • 25
  • 256
  • 373