[英]System.out.printf method throws a flags exception
I'm reading a documentation for the Formatter.format
and System.out.printf
methods (Java 6) and I'm trying to practice what I learned. 我正在阅读Formatter.format
和System.out.printf
方法(Java 6)的文档,我正在尝试练习我学到的东西。 I'm facing to an exception that seems to me unjustified, or there is a problem of understanding the topic. 我面临一个在我看来不合理的例外情况,或者存在理解该主题的问题。
The code I wrote is simple: 我写的代码很简单:
final int i = -15;
System.out.printf("%1$-+06d", i);
What I expect is having an int printed with the following format 我期望的是使用以下格式打印int
1) pad to left ; -
2) view the sign (négatif or positif); +
3) complete with left zeros; 0
4) print on 6 columns (characters); 6
Instead, an exception is thrown and I don't know why: 相反,抛出异常,我不知道为什么:
Exception in thread "main" java.util.IllegalFormatFlagsException: Flags = '-+0'
at java.util.Formatter$FormatSpecifier.checkNumeric(Formatter.java:2935)
at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2890)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2643)
at java.util.Formatter.parse(Formatter.java:2480)
at java.util.Formatter.format(Formatter.java:2414)
at java.io.PrintStream.format(PrintStream.java:920)
at java.io.PrintStream.printf(PrintStream.java:821)
at cert.simo.formats.Test.main(Test.java:18)
Any Explainations? 任何解释? Thank you. 谢谢。
"-" and "0" are incompatible options. “ - ”和“0”是不兼容的选项。 You either pad with zeros or align to the left. 您可以使用零填充或向左对齐。 Down below is part of java 6 source code (v6-b14) causing this exception. 下面是java 6源代码(v6-b14)的一部分导致此异常。
if ((f.contains(Flags.PLUS) && f.contains(Flags.LEADING_SPACE))
|| (f.contains(Flags.LEFT_JUSTIFY) && f.contains(Flags.ZERO_PAD)))
throw new IllegalFormatFlagsException(f.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.