Introduction
In this tutorial, we will learn how to skip or display % in a String using String.format
MyStringFormatter .java
public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.format( "Your score is %d % " ,65) ); } }
Output
Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ' ' at java.base/java.util.Formatter.checkText(Formatter.java:2732) at java.base/java.util.Formatter.parse(Formatter.java:2718) 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 MyStringFormatter.main(MyStringFormatter.java:4)
Solution
To display a % in String when using String.format, you have to escape a % with % i.e. %%
public class MyStringFormatter { public static void main(String[] args) { System.out.println(String.format( "Your score is %d %% " ,65) ); } }
Output
Your score is 65 %
Comments
Post a Comment