Blog |

How to Resolve the Illegal Format Conversion Exception in Java

How to Resolve the Illegal Format Conversion Exception in Java
Table of Contents

The IllegalFormatConversionException is an unchecked exception in Java that occurs when the argument that corresponds to a format specifier is of an incompatible type. Since the IllegalFormatConversionException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.


What Causes the Illegal Format Conversion Exception?

The IllegalFormatConversionException is thrown when an incompatible type argument is passed to a format specifier. For example, the %d format specifier requires an integer to be passed to it, and if a String is passed instead, an IllegalFormatConversionException is thrown.


Examples of the IllegalFormat Conversion Exception

Here is an example of an IllegalFormatConversionException thrown when a String is passed to a format specifier that expects an integer:

public class IllegalFormatConversionExceptionExample {
    public static void main(String args[]) {
        System.out.printf("%d", "Hello World");

Since the %d format specifier expects an integer and the actual value passed to it was of type String, running the above code throws the IllegalFormatConversionException:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
    at java.base/java.util.Formatter$FormatSpecifier.failConversion(
    at java.base/java.util.Formatter$FormatSpecifier.printInteger(
    at java.base/java.util.Formatter$FormatSpecifier.print(
    at java.base/java.util.Formatter.format(
    at java.base/
    at java.base/
    at IllegalFormatConversionExceptionExample.main(


How to Fix IllegalFormatConversionException

To avoid the IllegalFormatConversionException, it should be ensured that the argument passed to a format specifier is of the correct type. If the argument passed is correct, the format specifier should be checked to make sure it is correct for the passed argument, and fixed if necessary.

In the above example, the exception can be resolved by replacing the %d format specifier with the String format specifier %s:

public class IllegalFormatConversionExceptionExample {
    public static void main(String args[]) {
        System.out.printf("%s", "Hello World");

Running the above code produces the correct output as expected:

Hello World


Track, Analyze and Manage Errors With Rollbar

Rollbar in action

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape