JetBrains/intellij-community · error · FormatDecode.IllegalFormatException

zero padding flag ''0'' used but width not specified in ''{0

Error message

zero padding flag ''0'' used but width not specified in ''{0}''

What it means

Thrown when a specifier uses the zero-padding flag '0' but specifies no field width. Zero padding means 'pad with zeros up to width N'; without a width there is nothing to pad, so java.util.Formatter rejects it. FormatDecode applies the identical rule (ZERO_PAD set while width is empty) when validating format strings for the IG inspections.

Source

Thrown at java/java-analysis-impl/src/com/siyeh/ig/format/FormatDecode.java:374

        }
      }
      if (precision != null && precision.length() < 2) {
        throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.invalid.precision", specifier));
      }
      if (isAllBitsSet(flagBits, LEADING_SPACE | PLUS)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", ' ', '+', specifier));
      }
      if (isAllBitsSet(flagBits, LEFT_JUSTIFY | ZERO_PAD)) {
        throw new IllegalFormatException(
          InspectionGadgetsBundle.message("format.string.error.illegal.flag.combination", '-', '0', specifier));
      }
      if (StringUtil.isEmpty(width)) {
        if (isAllBitsSet(flagBits, LEFT_JUSTIFY)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.left.justify.no.width", specifier));
        }
        if (isAllBitsSet(flagBits, ZERO_PAD)) {
          throw new IllegalFormatException(InspectionGadgetsBundle.message("format.string.error.zero.padding.no.width", specifier));
        }
      }
      storeValidator(allowed, pos, parameters, argumentCount);
    }
    if (i < formatString.length()) {
      String endString = formatString.substring(i);
      if (!isPrefix) {
        checkText(endString);
      }
      else {
        //only case, when fsPattern doesn't find a specifier, is when there is no any conversion.
        //add "s" as a random reasonable conversion
        String suggestedString = endString + "s";
        Matcher endMatcher = fsPattern.matcher(suggestedString);
        if (!endMatcher.find() || endMatcher.end() != suggestedString.length()) {
          checkText(endString);
        }
      }

View on GitHub (pinned to be881553f2)

Solutions

  1. Add a width after the flags: "%05d" zero-pads an integer to 5 digits.
  2. If you only wanted to print the number unchanged, drop the '0' flag entirely: "%d".
  3. When constructing specifiers programmatically, emit the '0' flag only when a width is also emitted.

Example fix

// before
String s = String.format("%0d", 42);

// after
String s = String.format("%05d", 42);
Defensive patterns

Strategy: validation

Validate before calling

static void checkZeroPadHasWidth(String fmt) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("%(0)(\d+)?[a-zA-Z]").matcher(fmt);
  while (m.find()) {
    if (m.group(2) == null) throw new IllegalArgumentException("'0' without width: " + m.group());
  }
}

Prevention

When it happens

Trigger: A specifier such as "%0d" or "%05.1f"-style strings where the width got dropped, leaving '0' with no following width digits (e.g. "%0f"), decoded by FormatDecode.

Common situations: Deleting the width while editing a format string; building specifiers from flag and width variables where the width is empty; misreading '0' as part of the width rather than as a flag.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/5b98e008b378b40f. Report an issue: GitHub.