• Register
0 votes
167 views

Problem :

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

6 5 3
6,930 points

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 1
3,890 points

Related questions

1 vote
1 answer 10 views
10 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 stewart 4k points
0 votes
1 answer 262 views
262 views
Problem : I want to POST the List of custom objects. My JSON in a request body is as below : {     "collection": [         {             "name": "Test ordert1",             "detail": "ahk ksa"         },         {             "name": "Test ... } But I am facing below error: “Can not deserialize instance of java.util.arraylist out of start_object token” How can I fix the above error?
asked Jan 21 jwilliam 3.9k points
0 votes
1 answer 498 views
498 views
Problem : I am getting the run time exception in my program while I am reading a file through a Scanner. exception in thread "main" java.util.nosuchelementexception: no line found
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 4K views
4K views
Problem : I am trying to POST a List of custom objects but i am getting exception as below can not deserialize instance of java.util.arraylist out of start_object token
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 34 views
34 views
I am new to this. I just want to know how to do that? Help me.
asked Nov 7 TeamScript 1.8k points