Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.IllegalFormatConversionException

Tags:

java

I am having a little problem in my code here. I have no idea how I am supposed to fix it, and I tried some stuff, but I think, I'm not getting the message here, even though I suspect the issue to be some kind of elemental and easy to fix problem. The exceptions are below the code.

package test;
public class CircleExercise {

    public static void main(String[] args) {

        double[] rKreis = new double[3];

        for(int i = 1 ; i <= 3 ; i++){

            rKreis[i] = Double.parseDouble("4.9");

            System.out.printf("%n%d, Kreis: %nRadius: %d%nUmfang: %d%nFlaeche: %d%n",
                    i, rKreis[i], Circle.getCircumference(rKreis[i]), Circle.getArea(rKreis[i]));   
        }
    }
}

Exception is as follows

1, Kreis: 
Radius: Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:3999)
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2709)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2661)
    at java.util.Formatter.format(Formatter.java:2433)
    at java.io.PrintStream.format(PrintStream.java:920)
    at java.io.PrintStream.printf(PrintStream.java:821)
    at CircleExercise.main(CircleExercise.java:13)
like image 489
user3465376 Avatar asked Mar 26 '14 17:03

user3465376


People also ask

What is IllegalFormatConversionException Java?

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.

Is illegal class format exception checked or unchecked?

Unchecked exception thrown when a format string contains an illegal syntax or a format specifier that is incompatible with the given arguments. Only explicit subtypes of this exception which correspond to specific errors should be instantiated.


1 Answers

%d goes with integer in Java. Use %f instead in your printf()

Another useful info. If you use %.02f then it will print only two decimal point value after the dot .

like image 148
Sabuj Hassan Avatar answered Oct 10 '22 16:10

Sabuj Hassan