java.util.IllegalFormatConversionException in the String Format in java



In this Example we are going to understand the String Format for Strings.

Error Message :
Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
       at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4426)
       at java.base/java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2951)
       at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2898)
       at java.base/java.util.Formatter.format(Formatter.java:2673)
       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:10)

Error Description :

package com.si.Strings;

public class StrinFormat {

       public static void main(String[] args) {

             String str = "Surya Infoware";
              String str2 = "very ";
                    
              String str1 = String.format("My company name is : %.4f", str);
              System.out.println(str1);
       }

}

In the above program we get an error in the red background line.
In the String format we are printing string but we are giving format as float(%.4f).
Due to this we are getting IllegalFormatConversationException.


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 ";
                    
              String str1 = String.format("My company name is : %s", str);
              System.out.println(str1);
       }

}

Output:
My company name is : Surya Infoware   


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