19

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)
1
  • 1
    The exception tells you right here that you need to something other than %d: " java.util.IllegalFormatConversionException: d != java.lang.Double"
    – user2591612
    Commented Mar 26, 2014 at 17:29

3 Answers 3

41

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

4
  • It somehow didn't work the way you suggested. I just changed to normal "System.out.println". Thank you anyways. Commented Mar 26, 2014 at 17:48
  • 1
    This is how I am doing the formatting as @sabuj suggested and it's working.Double myTestScore = new Double(1000000); String myFormattedString = String.format("%,.0f", myTestScore); System.out.println(myFormattedString); Commented Mar 28, 2019 at 20:09
  • for double its also f? Commented Apr 2, 2021 at 7:45
  • 'f' for floating point - docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/… This will work for doubles (simulated with Kreis as 4th and 5ft parameters to bypass unsuplied Circle class): System.out.printf("%n%d, Kreis: %nRadius: %f%nUmfang: %f%nFlaeche: %f%n", i, rKreis[i], rKreis[i], rKreis[i]);
    – João
    Commented Feb 15, 2022 at 8:46
2

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

0

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.

5
  • 1
    "in the question case this is not an issue" How not? rKreis[i] is a double, it cannot be formatted using %d - as explained by the exception - and as already (correctly) answered more than 10 years ago !
    – user85421
    Commented Oct 26, 2024 at 13:32
  • @user85421 My answer is for when the variable is in String data type. Commented Oct 28, 2024 at 3:17
  • sure, but the question is about "IllegalFormatConversionException: d != java.lang.Double" and, by the way, your code will also throw an exception: "IllegalFormatConversionException: d != java.lang.String"
    – user85421
    Commented Oct 28, 2024 at 7:36
  • You are correct it should be "%3s", as it's String, thanks for bringing my attention back to the correct solution. Commented Oct 29, 2024 at 8:20
  • but you are still stating that "in the question case this is not an issue" :-| it is exactly the issue ("val is not an integer")
    – user85421
    Commented Oct 29, 2024 at 8:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.