java.util.UnknownFormatConversionException while String Format in Java


In this Example we are going to understand the String Format of strings and the errors

Error Message :
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '1'
       at java.base/java.util.Formatter.checkText(Formatter.java:2732)
       at java.base/java.util.Formatter.parse(Formatter.java:2708)
       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:2988)
       at com.si.Strings.StrinFormat.main(StrinFormat.java:9)

Error Description :

package com.si.Strings;

public class StrinFormat {

       public static void main(String[] args) {
              String str = "Surya Infoware";
              String str2 = "very good";
                    
              String str1 = String.format("My company name is : %1 %1$s and %2$s", str,str2);
              System.out.println(str1);
       }
}
In the above program we get an error in the red background line.
In the String format we are giving some wrong formats to print the strings.
Due to this we are getting UnkownFormatConversationException.


Solution:
 We need to give String related format like below.
package com.si.Strings;

public class StrinFormat {

       public static void main(String[] args) {
              String str = "Surya Infoware";
              String str2 = "very good";
             
              String str1 = String.format("My company name is : %1$s %1$s and %2$s", str,str2);
              System.out.println(str1);

       }
}

Output:
My company name is : Surya Infoware Surya Infoware and very good


Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar