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

- **ID:** `java/illegal-format-conversion`
- **Domain:** java
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 95%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Java 8 | active | — | — |
| Java 11 | active | — | — |
| Java 17 | active | — | — |
| Java 21 | active | — | — |

## Workarounds

1. **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.** (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.
   ```
2. **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)`** (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)`
   ```
3. **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)`** (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)`
   ```

## Dead Ends

- **Change the format specifier to %s to accept any type.** — 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. (60% fail)
- **Use `String.valueOf(arg)` to convert the argument before formatting.** — 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. (90% fail)
- **Catch the exception and retry with a different format.** — This only masks the error; the underlying type mismatch remains and may cause further issues in downstream logic. (70% fail)
