java runtime_error ai_generated true

java.util.IllegalFormatConversionException: d != java.lang.String

ID: java/illegal-format-conversion

Also available as: JSON · Markdown · 中文
95%Fix Rate
85%Confidence
1Evidence
2024-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Java 8 active
Java 11 active
Java 17 active
Java 21 active

Root Cause

A format specifier (e.g., %d for integer) is used with an argument of an incompatible type (e.g., String), causing a runtime exception when formatting strings via String.format or PrintStream.printf.

generic

中文

格式说明符(例如用于整数的 %d)与不兼容类型的参数(例如 String)一起使用,导致通过 String.format 或 PrintStream.printf 格式化字符串时发生运行时异常。

Official Documentation

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/IllegalFormatConversionException.html

Workarounds

  1. 95% success Ensure the argument type matches the format specifier. For %d, pass an integer: `String.format("Value: %d", 42)` instead of `String.format("Value: %d", "42")`. Convert strings to integers using Integer.parseInt() if necessary.
    Ensure the argument type matches the format specifier. For %d, pass an integer: `String.format("Value: %d", 42)` instead of `String.format("Value: %d", "42")`. Convert strings to integers using Integer.parseInt() if necessary.
  2. 95% success Use the correct format specifier for the argument type: %s for strings, %f for floats, %d for integers. Example: `String.format("Name: %s, Age: %d", name, age)`
    Use the correct format specifier for the argument type: %s for strings, %f for floats, %d for integers. Example: `String.format("Name: %s, Age: %d", name, age)`
  3. 85% success If the argument can be of multiple types, use a conditional to apply the correct format specifier: `String.format(arg instanceof Integer ? "%d" : "%s", arg)`
    If the argument can be of multiple types, use a conditional to apply the correct format specifier: `String.format(arg instanceof Integer ? "%d" : "%s", arg)`

中文步骤

  1. 确保参数类型与格式说明符匹配。对于 %d,传递整数:`String.format("Value: %d", 42)` 而不是 `String.format("Value: %d", "42")`。如有必要,使用 Integer.parseInt() 将字符串转换为整数。
  2. 为参数类型使用正确的格式说明符:字符串用 %s,浮点数用 %f,整数用 %d。示例:`String.format("Name: %s, Age: %d", name, age)`
  3. 如果参数可以是多种类型,使用条件语句应用正确的格式说明符:`String.format(arg instanceof Integer ? "%d" : "%s", arg)`

Dead Ends

Common approaches that don't work:

  1. Change the format specifier to %s to accept any type. 60% fail

    While %s works for strings, it produces the string representation of the object, which may not match the desired numeric formatting (e.g., padding, precision). This changes the output format unintentionally.

  2. Use `String.valueOf(arg)` to convert the argument before formatting. 90% fail

    If the argument is not a string, String.valueOf will still produce a string, but the format specifier %d will still expect an integer, causing the same exception.

  3. Catch the exception and retry with a different format. 70% fail

    This only masks the error; the underlying type mismatch remains and may cause further issues in downstream logic.