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)
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.
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.
%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 .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With