Java % 符号转换失败

标签 java string exception format

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

这里有一个异常(exception):

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 但抛出了相同的异常。请指教。

最佳答案

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

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

关于Java % 符号转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24939084/

相关文章:

java - Python 中的无限循环

java - 在 Jsoup 中使用 href 解析表类时出现问题

java - 嵌套泛型和通配符

java - JDK中有没有一个好的异常类来应对用户输入太长的情况?

java - java中错误和未经检查的异常之间的区别?

java - 有没有办法在 spring mvc3 的 xml 中设置 if/else 条件?

c# - String.Split 仅在 C# 中的第一个分隔符上?

c - fprintf 和 fputs 在将字符串写入文件时忽略字符串中的 '\'

c# - 用于选择包含所有字母的字符串的 Linq 语句

c++ - 为什么这个程序无法捕获异常?