java.util.UnknownFormatConversionException:

发布于 2024-12-20 15:53:39 字数 526 浏览 0 评论 0原文

  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)

知道出了什么问题吗?

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半城柳色半声笛 2024-12-27 15:53:39

问题出在第一行的%\n。请注意,% 是格式字符串中的特殊字符,表示后面有格式说明符。 % 后面的 \n 不是有效的格式说明符。

如果要打印百分号,请在格式字符串中将其加倍: %%

如果要打印换行符,则使用 %n,而不是 %\n

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.

眼睛会笑 2024-12-27 15:53:39

格式字符串中的问题是您混合了两种换行方式:%n\n。前者告诉格式化程序以平台需要的任何格式放置换行符,而后者仅放入文字换行符。但是你写的是%\n,这意味着你正在转义换行符,这就是爆炸的原因。

您还忘记了第二次调用中“String”和 243.08 之间的逗号。顺便说一句,%d 格式化一个整数,所以如果您尝试打印 243.08,您可能不需要它。

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.

但可醉心 2024-12-27 15:53:39

错误..

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

http://ideone.com/USOx1

Bugs..

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

http://ideone.com/USOx1

残疾 2024-12-27 15:53:39

就我而言(Android),strings.xml 中存在错误:

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

当我尝试获取 getString(R.string.cashback) 时,收到此异常。要修复此问题,请将 % 替换为 %%

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

奢望 2024-12-27 15:53:39

如果需要在 Kotlin 中格式化字符串,可以使用以下代码片段。

在 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>

在主代码中:

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

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
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文