java.util.IllegalFormatConversionException when using System.out.format()

A few weeks ago I described how to format output using System.out.printl() and System.out.println() methods

Try to run the code below:

double avgAge = 245 / 34;
System.out.format("Average age is %d.", avgAge);

Without doubts you’ll get an exception thrown at runtime:

Average age is Exception in thread "main"
java.util.IllegalFormatConversionException:
d != java.lang.Double
    at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)

The reason is becase wrong conversion label was used. To recall, conversion label is ‘%’ sign followed by a letter b, c, d, f, or s inside System.out.format().

In the above example, %d meant that an integral type (byte, Byte, short, Short, int, Integer, long, Long, BigInteger) would be used. However, finally there was a floating point passed (avgAge). This example would work if this conversion label were used: %f.

So, remember to be careful with conversion label!

Enjoy this Post?

Your vote will help me grow this site and provide even more information

4 Responses to “java.util.IllegalFormatConversionException when using System.out.format()”


  1. 1 Jeff

    Thanks soooooooooooo much Jarosław for this very helpful help.

  2. 2 deval mathur

    it was helpful….
    i was struck at it at one point….gud

  3. 3 Tahir

    Thanks, It is much clear and to the point.
    Precise and concise without any unnecessary stuff

  4. 4 Raja Nagendra Kumar

    As a addendum to the same error, we see the following can happen due to lack of conversion from long to float.

    for a code like this

    boolean lInMBSize = (sizeCount / CommonConst.MEGA_BYTES) > 0;
    StringBuffer sb = new StringBuffer();
    Formatter fm = new Formatter(sb);
    fm.format(“\n%-16s\t%-6d\t%15.2f %s\t%-16d”,
    ext,
    fileCount,
    lInMBSize ?
    (sizeCount / CommonConst.MEGA_BYTES) :
    (sizeCount / CommonConst.KB_BYTES),
    lInMBSize ? “MB” : “kb”,
    lineCount);
    return sb.toString();

    We are seeing similar error in this case for converting long to float..the value of long passed is 2..

    Hope long to be convertible to float as they are compatible.

    java.util.IllegalFormatConversionException: f != java.lang.Long
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:399
    9)
    at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2722)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2667)
    at java.util.Formatter.format(Formatter.java:2433)
    at java.util.Formatter.format(Formatter.java:2367)
    at com.tejasoft.ant.common.cm.CMInfo.toString(CMInfo.java:99)
    at java.lang.String.valueOf(String.java:2826)

Leave a Reply




Switch to our mobile site