[java] Java: Literal percent sign in printf statement

I'm trying to add an actual percent sign into a printf statement in Java and I'm getting the error:

lab1.java:166: illegal escape character
                System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\n",ID,pattern,support,confidence);
                                                 ^
lab1.java:166: illegal escape character
                System.out.printf("%s\t%s\t%1.2f\%\t%1.2f\%\n",ID,pattern,support,confidence);
                                                          ^
2 errors

I can't figure out how to put an actual percent sign into my printf? I thought using \% to escape it would work, but it isn't.

Any ideas?

This question is related to java printf escaping

The answer is


Escaped percent sign is double percent (%%):

System.out.printf("2 out of 10 is %d%%", 20);

You can use StringEscapeUtils from Apache Commons Logging utility or escape manually using code for each character.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to printf

How I can print to stderr in C? How can you print multiple variables inside a string using printf? Unsigned values in C How to print multiple variable lines in Java Scanf/Printf double variable C Format specifier %02x %i or %d to print integer in C using printf()? What is the printf format specifier for bool? printf() prints whole array Printf width specifier to maintain precision of floating-point value

Examples related to escaping

Uses for the '"' entity in HTML Javascript - How to show escape characters in a string? How to print a single backslash? How to escape special characters of a string with single backslashes Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence Properly escape a double quote in CSV How to Git stash pop specific stash in 1.8.3? In Java, should I escape a single quotation mark (') in String (double quoted)? How do I escape a single quote ( ' ) in JavaScript? Which characters need to be escaped when using Bash?