• Register
0 votes
499 views

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));

Output:

1.330000

 1.330000

 1.330000

 1.330000

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);

Output:

[12.35]

 [12.35]

 [ 12.35]

 [  12.35]

 [12.35  ]

 [12.3000]

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

Example:

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

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

Output:

[1234.]

 [1234]

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: Can someone please guide me how to resolve > Exception in thread "main" java.util.illegalformatconversionexception: f != java.lang.integer
asked Apr 24 ummeshani 10.8k points
0 votes
1 answer 111 views
0 votes
1 answer 3 views
3 views
Problem: How to resolve this error ? >Error: internal error: no device with bus 'ide' and target 'hdc'
asked May 4 ochena 23.3k points
0 votes
1 answer 2 views
2 views
Problem: Why is xCode reserving so much disk space when running?
asked Apr 18 Humaira ahmed 50.7k points
1 vote
1 answer 30 views
30 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 16 views
16 views
Problem: HELP! I need help understanding this error > Java.lang.classcastexception: java.util.date cannot be cast to java.sql.date
asked May 1 ummeshani 10.8k points
0 votes
1 answer 5 views
5 views
Problem: Please help me fix this dilemma: which data type has more precision? a. double b. float c. int d. long int?
asked Mar 30 tuhin1 48.9k points
0 votes
1 answer 5 views
5 views
Problem: I am trying to write a JUnit test case for the same purpose. But on line Session session = Session.getDefaultInstance(properties); I am getting the following exception: java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
asked Apr 24 ummeshani 10.8k points
1 vote
1 answer 1 view
1 view
Problem: I am facing this error in my code : Exception in thread “main” Java.util.missingformatargumentexception: format specifier '10s' Here’s my code : package B00166353_Grades; public class Student{ String name,banner; public Student(String name,String banner){ ... String.format("%-20s%10.2s%10s",this.name,this.banner); return productDetails; } } How can I solve this problem ?
asked 11 hours ago Raj Tyagi 15.6k points
0 votes
1 answer 14 views
14 views
Problem: I use Eclipse Oxygen.2 Release (4.7.2) and try to develop a pen & paper character creation program as an exercise to learn to work with different files like xml and properties. My ResBundle.properties-file looks like this: cc.male=male cc.female= ... build path as a source folder (with Eclipse). After that I coul'd continue programming until I got the Exception message described above.
asked May 2 anika11 32.2k points