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]