user3465376
user3465376

Reputation: 193

java.util.IllegalFormatConversionException

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)

Upvotes: 19

Views: 42077

Answers (3)

Priyanshi Pandya
Priyanshi Pandya

Reputation: 47

Also, this exception occurs when your val is not an integer, in the question case this is not an issue, but just answering for other people's help.

String val = "100"; String.format("%3s", val);

To resolve make sure you parse String to int.

Upvotes: 0

Rajan singh
Rajan singh

Reputation: 59

I have same issue and solve like this

 if (allRec.get(i).getType() == 2) {
                        totalExpense = totalExpense.add(new BigDecimal**(String.format("%.2f",allRec.get(i).getAmount()))**);
                        tvExpense.setText("-" + moneySym + totalExpense + "");
                    }

  if (allRec.get(i).getType() == 1) {
                        // it throws number format exception
                        totalIncome = totalIncome.add(new **BigDecimal(String.format("%.2f",(allRec.get(i).getAmount()))));**
                        tvIncome.setText(moneySym + totalIncome + "");
                    }

String format table

Upvotes: 2

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

%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 .

Upvotes: 41

Related Questions