[Solved] java.util.IllegalFormatPrecisionException

In this post, we will learn about java.util.IllegalFormatPrecisionException in Java. It is an Unchecked exception. It is thrown when the precision is a negative value other than -1, the conversion does not support a precision, or the value is otherwise unsupported. First, we will produce the java.util.IllegalFormatPrecisionException before moving on to the solution.

Read Also: [Solved] java.util.IllegalFormatConversionException in Java

[Fixed] java.util.IllegalFormatPrecisionException

Example 1: Producing the exception by using %.2d format specifier for double data type


We can easily produce this exception by using %.2d format specifier for double data type as shown below:

public class IllegalFormatPrecisionExceptionExample {
    public static void main(String args[]) {
      // Initializing variables a and b    
      int a=10;
      double b=25;
      /* Below line will throw IllegalFormatPrecisionException
      as we are trying to print "b" double data type with %.2d
      format specifier */            
System.out.printf("%d, %.2d ",a,b);
} }

Output:
Exception in thread "main" java.util.IllegalFormatPrecisionException: 2


Explanation:

%d represents decimal integer and does not use a decimal point at all. The root cause of this exception is that you cannot format integers by using a decimal point in the conversion. Since b is a double, you can use floating-point conversion. So, the correct format specifier to be used here is %f (decimal floating-point). In the format specifier, the number after the decimal point indicates how many decimal places to go to.

Solution:

The above runtime exception can be resolved by replacing %.2d format specifier with %.2f as shown below:

public class IllegalFormatPrecisionExceptionExample {
    public static void main(String args[]) {
      // Initializing variables a and b    
      int a=10;
      double b=25;
      /* Below line will not throw IllegalFormatPrecisionException
      as we are trying to print "b" double data type with %.2f
      format specifier */            
System.out.printf("%d, %.2f ",a,b);
} }

Output:
10, 25.00


Example 2: Producing the exception by using %.2d format specifier for int data type


We can easily produce this exception by using %.2d format specifier for int data type as shown below:

public class IllegalFormatPrecisionExceptionExample2 {
    public static void main(String args[]) {
      // Initializing variables a and b    
      int a=10;
      int b=25;
      /* Below line will throw IllegalFormatPrecisionException
      as we are trying to print "b" int data type with %.2d
      format specifier */            
System.out.printf("%d, %.2d ",a,b);
} }

Output:
Exception in thread "main" java.util.IllegalFormatPrecisionException: 2


Explanation:

As we already know, you can not format integers by using a decimal point in the format specifier. Since b is an int data type, you can use %d (decimal integer) format specifier instead of %.2d.

Solution:

The above runtime exception can be resolved by replacing %.2d format specifier with %d as shown below:

public class IllegalFormatPrecisionExceptionExample2 {
    public static void main(String args[]) {
      // Initializing variables a and b    
      int a=10;
      int b=25;
      /* Below line will not throw IllegalFormatPrecisionException
      as we are trying to print "b" int data type with %d
      format specifier */            
System.out.printf("%d, %d ",a,b);
} }

Output:
10, 25


That's all for today. Please mention in the comments in case you are still facing the exception java.util.IllegalFormatPrecisionException in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry