gpt4 book ai didi

Java % 符号转换失败

转载 作者:行者123 更新时间:2023-11-30 03:48:45 25 4
gpt4 key购买 nike

我正在尝试对字符串进行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com