Saurabh Kumar
Saurabh Kumar

Reputation: 16661

java.util.UnknownFormatConversionException:

  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?

Upvotes: 26

Views: 94096

Answers (5)

Deniz Babat
Deniz Babat

Reputation: 316

If a string desired to format in Kotlin, the following code snippet can be used.

In the string.xml:

<string name = "exampleString"><![CDATA[<font color=#3177a3> String_1: <b>%%s</b><br> String_2: <b>%%s</b><br> String_3: <b>%%s</b></font>]]></string>

In the main code:

val format = String.format(getString(R.string.exampleString),
            value1,
            value2,
            value3
        )

Upvotes: 0

CoolMind
CoolMind

Reputation: 28793

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 %%.

Upvotes: 4

Jesper
Jesper

Reputation: 206816

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.

Upvotes: 36

yshavit
yshavit

Reputation: 43391

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.

Upvotes: 9

FailedDev
FailedDev

Reputation: 26930

Bugs..

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

http://ideone.com/USOx1

Upvotes: 7

Related Questions