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(Formatter.java:4426)
    at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2938)
    at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2892)
    at java.base/java.util.Formatter.format(Formatter.java:2673)
    at java.base/java.io.PrintStream.format(PrintStream.java:1209)
    at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
    at IllegalFormatConversionExceptionExample.main(IllegalFormatConversionExceptionExample.java:3)

 

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

Build with confidence. Release with clarity.

Rollbar helps you track what breaks, understand why, and improve what comes next.

5K free events per month, forever
14-day full feature trial
Easy and quick installation
Get started in minutes

Plans starting at $0. Take off with our 14-day full feature Free Trial.