Tag Archives: Java output text formatting

[Solved]java.util.UnknownFormatConversionException: Conversion = ‘I’


java.util.UnknownFormatConversionException is runtime unchecked exception which throw 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.

  • UnknownFormatConversionException is sub class of IllegalFormatException.

Constructors

  • UnknownFormatConversionException(String s) : Constructs an instance of this class with the unknown conversion.

Example of UnknownFormatConversionException

In this below example by mistake use “Index” with specifiers sign (%) where considering ‘I’ as specifiers which is not match with predefined specifiers that’s what throwing this exception.

try
{
Scanner sc = new Scanner(new File("C:\Users\saurabh.gupta1\Desktop\testdata.txt"));
int i=0;
while (sc.hasNextLine()) {
//System.out.println(sc.nextLine());
Scanner lineScanner = new Scanner(sc.nextLine());
lineScanner.useDelimiter("|");
if(i==0)
{
System.out.format("%-10Index%-10s%-10s%-10s%-10sn", sc.next(),sc.next(),sc.next(),sc.next(),sc.next());
}
else
{
i++;
System.out.format("%-10d%-10s%-10s%-10s%-10sn", i,sc.next(),sc.next(),sc.next(),sc.next(),sc.next());
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}

Output

java.util.UnknownFormatConversionException: Conversion = 'I'
at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at com.userinput.JavaScannerParsingExample.main(JavaScannerParsingExample.java:45)

Solution

Always follow formatting specifiers rules as given in Java formatter.

https://docs.oracle.com/javase/10/docs/api/java/util/Formatter.html

References

https://docs.oracle.com/javase/8/docs/api/java/util/UnknownFormatConversionException.html

Know More

To know more about Java Exception Hierarchy, in-built exception , checked exception, unchecked exceptions and solutions. You can learn about Exception Handling in override methods and lots more. You can follow below links:  s

[Solved] java.util.UnknownFormatConversionException: Conversion = ‘y’


java.util.UnknownFormatConversionException  is unchecked runtime exception which is sub class of java.util.IllegalFormatException. This exception occurs while using unknown parameter values while formatting data.

Constructors

  • UnknownFormatConversionException(String message) : Constructs a instance of this class with unknown conversion  message.

Sample Code

here in below example for extract values of date, day, month, year, hour and minute values from LocalDateTime and printing in particular format bySystem.out.printf()

System.out.println("*********Get Date and Time Values***************");
LocalDateTime dateTimePoint=LocalDateTime.now();
LocalDate datePoint = dateTimePoint.toLocalDate();
Month month = dateTimePoint.getMonth();
int day = dateTimePoint.getDayOfMonth();
int year = dateTimePoint.getYear();
int hour = dateTimePoint.getHour();
int minute = dateTimePoint.getMinute();
int second = dateTimePoint.getSecond();
System.out.println("Local Date :" + datePoint);
System.out.printf("Local Date time Format : %d/%d/%d %d:%d:%y", day, month.getValue(), year, hour, minute,second);

Output Message


Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'y'
    at java.util.Formatter$FormatSpecifier.conversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)

Issue

Here throw UnknowFormatConversionException for parameter value as y for printing values of second. it is because of just typing mistake. But in below solution mentioned on what cases it can occurd and how to resolve it.

Solutions

To solve this issue replace System.out.printf value y as d because for representing numeric value param value is d.

Follow below links to know about supported conversion format and parameters

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax

More Issues Solution

For more other JAVA/JDBC issues solution follow link JAVA/JDBC Issues.