• Register
search
Log In
0 votes
86 views

Problem :

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

6.9k 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.

36.1k 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]

 

3.9k points

Related questions

0 votes
1 answer 85 views
85 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 195 views
195 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 2.1K views
2.1K 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
1 vote
1 answer 14 views
14 views
Problem: Well, Java is a fun programming language to learn. I am adopting this language very quickly, I guess. I am wondering if I can generate a random double between 0 and 1 in java. Is it possible? Could you guys shed some light on this? Many Thanks!
asked Apr 6 Gavin 15k points
0 votes
1 answer 11 views
11 views
I am a noob in java, I have a task to cast a double to an int in java and I wrote a program to solve this: public static void main(String args[]) { double data = 3452.345; int value = data; System.out.println(value); } But it gives an error: Exception ... \Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) What is the problem in my code?
asked Mar 15 LizzyM 5.7k points