【问题标题】:Java conversion of % sign failing% 符号的 Java 转换失败
【发布时间】:2014-07-24 16:21:22
【问题描述】:

我正在尝试对字符串进行 string.format 并生成文字 % 我正在使用 2 个符号。但是java仍然无法识别它,我不知道为什么。它尝试使用以下字符作为转换,但失败了。

这里有一个例外:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'tX'
at java.util.Formatter$FormatSpecifier.checkDateTime(Unknown Source)
at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.lang.String.format(Unknown Source)
at files.CreateFilters.main(CreateFilters.java:218)

我尝试使用 unicode,但抛出了相同的异常。请指教。

【问题讨论】:

  • 如何制作一个简短但完整的程序来演示问题? (有问题的字符串甚至不需要稍微那么长......)
  • 为什么不用StringBuilder?
  • String.format 的代码在哪里?
  • 感谢@Braj 的提示,我最终做到了,而且效果很好。请回复,以便我解决这个问题。

标签: java string exception format


【解决方案1】:

如果我理解您的问题,正确的转义是双倍 \\(不是双倍百分比,但是在您的用例中,您实际上可能需要将其加倍到 \\\\),就像这样 -

final static String segmentClassLines = 
  "package com.nedstat.reporting.filters.cmf.%s.visitor%s;\n" + 
  "\n" + 
  "import com.nedstat.datamodel.db.sitestat.enums.ItemType;\n" + 
  "import com.nedstat.parameters.StringParameter;\n" + 
  "import com.nedstat.reporting.filters.ParameterTypeAlias;\n" + 
  "import com.nedstat.reporting.filters.VisitSelector;\n" + 
  "import com.nedstat.reporting.filters.annotation.FilterInfo;\n" + 
  "import com.nedstat.reporting.filters.annotation.Group;\n" + 
  "import com.nedstat.reporting.filters.annotation.Option;\n" + 
  "import com.nedstat.reporting.filters.annotation.Parameter;\n" + 
  "\n" + 
  "/**\n" + 
  " * generated by automated tool\n" + 
  " */\n" + 
  "\n" + 
  "@FilterInfo(id = %s, type = ItemType.STANDARD, name = \"%s\", groups = {\n" + 
  "    @Group(name = \"%s\", template = \"%s\", parameters = {\n" + 
  "        @Parameter(type = ParameterTypeAlias.ENUM, options = {\n" + 
  "            @Option(value = \"VisitSelector.FIRST\", description = \"\\%TXT_SEGMENTATION_FILTER_OPTION_FIRST\"),\n" + 
  "            @Option(value = \"VisitSelector.LAST\", description = \"TXT_SEGMENTATION_FILTER_OPTION_LAST\"),\n" + 
  "            @Option(value = \"VisitSelector.ANY\", description = \"TXT_SEGMENTATION_FILTER_OPTION_ANY\"),\n" + 
  "            @Option(value = \"VisitSelector.EVERY\", description = \"TXT_SEGMENTATION_FILTER_OPTION_EVERY\") }),\n" + 
  "         @Parameter(type = ParameterTypeAlias.WILD_CARD) }) })\n" + 
  "public class %s extends com.nedstat.reporting.filters.cmf.AbstractRibVisitorFilter\n" + 
  "{\n" + 
  "  public %s(VisitSelector visitSelector, StringParameter labelValue)\n" + 
  "  {\n" + 
  "    super(visitSelector, %s.class, labelValue);\n" + 
  "  }\n" + 
  "}\n";

当我打印那个字符串时,我得到了

package com.nedstat.reporting.filters.cmf.%s.visitor%s;

import com.nedstat.datamodel.db.sitestat.enums.ItemType;
import com.nedstat.parameters.StringParameter;
import com.nedstat.reporting.filters.ParameterTypeAlias;
import com.nedstat.reporting.filters.VisitSelector;
import com.nedstat.reporting.filters.annotation.FilterInfo;
import com.nedstat.reporting.filters.annotation.Group;
import com.nedstat.reporting.filters.annotation.Option;
import com.nedstat.reporting.filters.annotation.Parameter;

/**
 * generated by automated tool
 */

@FilterInfo(id = %s, type = ItemType.STANDARD, name = "%s", groups = {
    @Group(name = "%s", template = "%s", parameters = {
        @Parameter(type = ParameterTypeAlias.ENUM, options = {
            @Option(value = "VisitSelector.FIRST", description = "\%TXT_SEGMENTATION_FILTER_OPTION_FIRST"),
        @Option(value = "VisitSelector.LAST", description = "TXT_SEGMENTATION_FILTER_OPTION_LAST"),
        @Option(value = "VisitSelector.ANY", description = "TXT_SEGMENTATION_FILTER_OPTION_ANY"),
        @Option(value = "VisitSelector.EVERY", description = "TXT_SEGMENTATION_FILTER_OPTION_EVERY") }),
     @Parameter(type = ParameterTypeAlias.WILD_CARD) }) })
public class %s extends com.nedstat.reporting.filters.cmf.AbstractRibVisitorFilter
{
  public %s(VisitSelector visitSelector, StringParameter labelValue)
  {
    super(visitSelector, %s.class, labelValue);
  }
}

【讨论】:

  • @nuno_cruz 此外,在第 19 行的结果文本中,TXT_SEGMENTATION_FILTER_OPTION_FIRST 之前有一个 %%。你的意思是写%s
  • @jzelenkov 你应该重新阅读问题的第一句话。
  • ...哦,是的,谢谢。我误解了@nuno_cruz 的问题。所以 OP 实际上想在那个地方有一个% 标志?!这让我很困惑。什么可能是一个用例?据我了解%s 稍后将被替换,但% 单独?有什么想法吗?
  • @jzelenkov 我认为它应该表明该列是一个百分比。比如15%、25%。所以一个文字 % 符号。
  • @ElliottFrisch 是对的,我希望出现一个文字 % 符号。但这对我也不起作用。
【解决方案2】:

事实证明,使用 StringBuilder 是处理更大字符串的更好解决方案。

【讨论】:

    猜你喜欢
    • 2013-02-01
    • 2020-04-16
    • 2015-01-30
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 2012-07-31
    • 2019-10-20
    相关资源
    最近更新 更多