Java UnknownFormatConversionException | Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '-', = '#', = '.' - Solution

UnknownFormatConversionException in Java. 

In this post, We will learn about UnknownFormatConversionException with some examples.

Unchecked exception thrown when an unknown conversion is given

Unless otherwise specified, passing a null argument to any method or constructor in this class will cause a NullPointerException to be thrown.

NullPointerException in String contains method.

UnknownFormatConversionException is child class of IllegalFormatException class.


UnknownFormatConversionException


UnknownFormatConversionException Constructor Syntax:

public UnknownFormatConversionException(String s)


Constructs an instance of this class with the unknown conversion. s indicates the unknown conversion type or value.

UnknownFormatConversionException Methods:

This has 2 methods as below.

1) public String getConversion(): Returns the unknown conversion.
2) public String getMessage(): the detail message string of this Throwable instance (which may be null). This method is overridden method from Throwable class.

Example program on UnknownFormatConversionException:

When we will get exception UnknownFormatConversionException:

The following code throws this exception, and I'm not sure why. There is clearly a problem with outputting String.format to the formatedStr variable, but I don't know what's wrong with it.


package examples.java.w3schools.exceptions;

public class UnknownFormatConversionException {

public static void main(String[] args) {
String name = "Jhon";
int age = 20;

String formatedStr = String.format("Name : %s, Age : %i", name, age);
System.out.println("formatedStr : " + formatedStr);
}
}

Output:

Below is the exception full stack trace.


Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'i'
at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839)
at java.base/java.util.Formatter$FormatSpecifier.(Formatter.java:2865)
at java.base/java.util.Formatter.parse(Formatter.java:2713)
at java.base/java.util.Formatter.format(Formatter.java:2655)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:2897)
at w3schools/examples.java.w3schools.exceptions.UnknownFormatConversionException.main(UnknownFormatConversionException.java:9)


We will understand where the problem is. Just observe the reason for the exception that is "Conversion = 'i'". It is indicating that some problem with conversion type i.
In above code, used "%i" for integer which is causing for the error. 

Solution:

To resolve the issue, we should reformat the code as below. For integer, must use 'd' instead of 'i'. This will resolve the problem.
String formatedStr = String.format("Name : %s, Age : %d", name, age);

Output:

The above code will produce this output.

formatedStr : Name : Jhon, Age : 20

Another example program on UnknownFormatConversionException:

In the below code snippet, we are trying to say the welcome to the Billy and used "%#" for type String content. We will run and see the output.
   String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog :  ", "Billy");
System.out.println("Welcome Note : " + welcomeNote);

Output:

See the below output and seeing the conversion '#' is causing the problem which is invalid conversion type. To resolve, should use corresponding type that is "%s" in this case.
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '#'
at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839)
at java.base/java.util.Formatter$FormatSpecifier.(Formatter.java:2865)
at java.base/java.util.Formatter.parse(Formatter.java:2713)
at java.base/java.util.Formatter.format(Formatter.java:2655)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:2897)
at w3schools/examples.java.w3schools.exceptions.UnknownFormatConversionException.main(UnknownFormatConversionException.java:13)

UnknownFormatConversionException getConversion method example:

UnknownFormatConversionException-getMessage


This method returns the what conversion value causes for the problem. Refer the below example program.


public class UnknownFormatConversionExceptionMethods {

public static void main(String[] args) {

try {
String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog : ", "Billy");
System.out.println("Welcome Note : " + welcomeNote);
} catch (java.util.UnknownFormatConversionException ex) {
System.out.println("getConversion method value: " + ex.getConversion());
}
}
}


getConversion method Output:

getConversion method value: #


UnknownFormatConversionException getMessage method example:

getMessage method will provide the complete detailed reason for the error. Always better to go with this method rather than getting the conversion by invoking getConversion() method.

package examples.java.w3schools.exceptions;

public class UnknownFormatConversionExceptionMethods {

public static void main(String[] args) {

try {
String welcomeNote = String.format("Hello, Mr %# : Welcome to JavaW3schools blog : ", "Billy");
System.out.println("Welcome Note : " + welcomeNote);
} catch (java.util.UnknownFormatConversionException ex) {
System.out.println("getMessage method value: " + ex.getMessage());
}
}
}


getMessage method Output:


getMessage method value: Conversion = '#'

0 Comments