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)
知道出了什么问题吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题出在第一行的
%\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
.格式字符串中的问题是您混合了两种换行方式:
%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.错误..
http://ideone.com/USOx1
Bugs..
http://ideone.com/USOx1
就我而言(Android),
strings.xml
中存在错误:当我尝试获取
getString(R.string.cashback)
时,收到此异常。要修复此问题,请将%
替换为%%
。In my case (Android) there was an error in
strings.xml
:When I tried to get
getString(R.string.cashback)
, I received this exception. To fix it, replace%
with%%
.如果需要在 Kotlin 中格式化字符串,可以使用以下代码片段。
在 string.xml 中:
在主代码中:
If a string desired to format in Kotlin, the following code snippet can be used.
In the string.xml:
In the main code: