0

Here is simple string format and it works.

String format = "Your name is %s";
String text = String.format(format, "Harry");

System.out.print(text);//print "Your name is Harry"

But when I use "%" in the same line

String format = "Your name is %s, 100%point";
String name = String.format(format, "Harry");

System.out.print(name);// I want to print "Your name is Harry, 100%point"

I want to print "Your name is Harry, 100%point",

but it come into runtime error

 java.util.UnknownFormatConversionException: Conversion = 'p'
        at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2781)
        at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2811)
        at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2624)
        at java.util.Formatter.parse(Formatter.java:2557)
        at java.util.Formatter.format(Formatter.java:2504)
        at java.util.Formatter.format(Formatter.java:2458)
        at java.lang.String.format(String.java:2842)

How to solve it. Any help will be appreciate.

GHH
  • 1,713
  • 1
  • 8
  • 21

1 Answers1

0

You need to escape % with another %, i.e. %%.

String format = "Your name is %s, 100%%point";
String name = String.format(format, "Harry");

System.out.print(name); // I want to print "Your name is Harry, 100%point"
devang
  • 5,376
  • 7
  • 34
  • 49