Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.UnknownFormatConversionException:

Tags:

java

  System.out.printf("%s%13s%\n", "TarrifType", "AnnualCost");
  System.out.printf("%s%d.%n", "String" 243.08);

    Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '
at java.util.Formatter.checkText(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at ModelComparison.main(ModelComparison.java:12)

Any idea whats wrong?

like image 535
Saurabh Kumar Avatar asked Dec 08 '11 20:12

Saurabh Kumar


4 Answers

What's wrong is the %\n in the first line. Note that the % is a special character in a format string that indicates that a format specifier follows. The \n after the % is not a valid format specifier.

If you wanted to print a percent sign, then double it in the format string: %%

If you wanted to print a newline, then use %n, not %\n.

like image 81
Jesper Avatar answered Oct 02 '22 02:10

Jesper


The problem in your format string is that you mixed two ways of doing newline: %n and \n. The former tells the formatter to put a newline in whatever format the platform requires, whereas the latter puts in just a literal newline char. But what you wrote was %\n, which means you're escaping the newline char, and that's what's blowing up.

You also forgot a comma between "String" and 243.08 in the second call. And btw, %d formats an integer, so you probably don't want it if you're trying to print 243.08.

like image 31
yshavit Avatar answered Oct 02 '22 03:10

yshavit


Bugs..

System.out.printf("%s%13s\n", "TarrifType", "AnnualCost");
System.out.printf("%s%f\n", "String", 243.08);

http://ideone.com/USOx1

like image 43
FailedDev Avatar answered Oct 02 '22 02:10

FailedDev


In my case (Android) there was an error in strings.xml:

<string name="cashback">10% cashback</string>

When I tried to get getString(R.string.cashback), I received this exception. To fix it, replace % with %%.

like image 45
CoolMind Avatar answered Oct 02 '22 03:10

CoolMind