• Register
0 votes

Problem :

I am getting bellow error
java.util.illegalformatconversionexception: d != java.lang.double

6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

You must have got a format specifier in the wrong section of the printf statement. So it needs to be in the String that is part of the first parameter, before the comma that separates method parameters. 

So not this:

System.out.printf("x = "+x+", y = %d", y);//incorrect

but rather this:

System.out.printf("x = %d, y = %d", x, y);//correct

or for new line:

System.out.printf("x = %d, y = %d%n", x, y); 

Please note in a printf or String.format(...) statement, use %n not \n for new-lines.

9 7 4
38,600 points
0 votes

This error occurs when your format specifier is wrong in printf statement. Java.util.Formatter is an interpreter for getting the C language printf style formatted strings. In java we usually use two methods to format console output which internally uses Formatter class:

System.out.prinf(String format, Object… args)

System.out.printf(Locale 1, String format, Object… args)

The format method usually consists of one or multiple formatting specifiers. A formatting specifier starts with % which is a way to specify various formatting attributes to get the desired results.

f- float/double formatting:

System.out.printf("%f%n", 1.33f);

 System.out.printf("%f%n", 1.33d);

 System.out.printf("%f%n", Double.valueOf(1.33d));

 System.out.printf("%f%n", BigDecimal.valueOf(1.33d));






Applying precision:

Syntax: x.y, where x is the width also called padding, and y is the decimal places. The value of x is ignored if it is smaller than the necessary chars including the decimal values to display. Remember x is not the limited width but to add the padding. Y is used to increase or decrease the decimal places.

System.out.printf("[%4.2f]%n", 12.34567);

 System.out.printf("[%5.2f]%n", 12.34567);

 System.out.printf("[%6.2f]%n", 12.34567);

 System.out.printf("[%7.2f]%n", 12.34567);

 System.out.printf("[%-7.2f]%n", 12.34567);

 System.out.printf("[%7.4f]%n", 12.3);

 System.out.printf("[%8.4f]%n", 12.3);




 [ 12.35]

 [  12.35]

 [12.35  ]


 [ 12.3000]

Display decimal with # flag:

The integer portion of the results always ends with a decimal point, even if the fractional part is zero.


System.out.printf("[%#1.0f]%n", 1234d);

 System.out.printf("[%1.0f]%n", 1234d);





11 5 2
3,890 points

Related questions

0 votes
1 answer 4 views
Problem: I am new to programming so I apologize beforehand. I am running a program and am having a problem with the System.out.printf method in Java. I know everything else in the program is working correctly (tested it). However this does not ... Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Advisor_Score.main(Advisor_Score.java:17)
asked 4 days ago Ali 4.3k points
0 votes
1 answer 5 views
Problem: inputs the amount of minutes the user enters and calculates the grand total by adding the CostForMinutes and BasePlanCost. CostForMinutes is calculated by multiplying the minutes the user enters and the BaseCostPerMinute. The out is all the numbers outputted ... this error exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String can anyone help me out?
asked 2 days ago Ali 4.3k points
0 votes
1 answer 11 views
1 vote
1 answer 17 views
Problem : I am very new to the File Systems. Currently I am interested in knowing the correct difference between the internal and the external fragmentation while allocating the disk space for the files?
asked Apr 16, 2020 stewart 4k points
0 votes
1 answer 6 views
Problem: failed to convert value of type java.util.hashmap to string.
asked 5 days ago Ethan ross 1.8k points
0 votes
1 answer 6 views
Problem: I have two properties records : config-app. properties config-dev.properties what's more, I attempt to peruse from the clenched hand witch read additionally from the second that way : the java code: String smtpHostName = ResourceBundle.getBundle("config-app").getString("smtp.host.name");
asked 6 days ago TeamScript 13.5k points
0 votes
1 answer 5 views
Problem: I am getting the below exception when I run my mvn install. I have even deleted the local repository and ran again getting same exception. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.1:shade (default) on project cores-batch: Error creating shaded jar: invalid LOC header (bad signature) -> [Help 1]
asked Feb 17 Ali 4.3k points
0 votes
1 answer 16 views
I am using MySQL database to store data. can anyone help me?
asked Feb 14 TeamScript 13.5k points
0 votes
1 answer 10 views
I'm obviously missing something but I don't know what.
asked Feb 11 TeamScript 13.5k points
0 votes
1 answer 30 views