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!
Thanks soooooooooooo much Jarosław for this very helpful help.
it was helpful….
i was struck at it at one point….gud
Thanks, It is much clear and to the point.
Precise and concise without any unnecessary stuff
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)
thaaaanks, my friend got an error and i couldnt figure what he had done wrong
i knew it had something to do with the double.
import java.io.*;
class DemoReadline
{
public static void main(String args[]) throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println(“enter id of the employee”);
int id = Integer.parseInt(br.readLine());
System.out.println(“enter sex of the employee”);
char ch=(char)br.read();
System.out.println(“enter name of the employee”);
String name= br.readLine();
System.out.printf(“num=%d%nchar=%c%nString=%s%n”+id,ch,name);
}
} wats the right syntax here,,,can anybody help me out….am a learner of java…its the exception…enter id of the employee
2
enter sex of the employee
m
enter name of the employee
num=Exception in thread “main” java.util.IllegalFormatConversionException: d != java.lang.Character
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)
at java.io.PrintStream.printf(Unknown Source)
at DemoReadline.main(DemoReadline.java:13)
Thanks for help!