%符号的Java转换失败

时间:2014-07-24 16:21:23

标签: java string exception format

我正在尝试使用字符串的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,但抛出相同的异常。请指教。

2 个答案:

答案 0 :(得分:2)

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

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);
  }
}

答案 1 :(得分:0)

使用StringBuilder可以更好地解决更大的字符串问题。