Java UnknownFormatConversionException:Conversion =' m'

时间:2017-04-26 13:25:09

标签: java format type-conversion

我有这样的代码

if (jcb_rtsplit.isSelected()) {
        ArrayList<String> temp = P.RT_Split(dataTraining);
        writeFile(temp);
        cekRTSplit = true;
        cekRT = true;
    }

和代码方法writefile()如下:

public void writeFile(ArrayList<String> temp){
    CreatingFile file = new CreatingFile();
    file.openFile("RT Split.txt");
    for (int i = 0; i < temp.size(); i++) {
        file.addRecord(temp.get(i));
    }
    file.close();
}

这是代码CreatingFile,如下所示:

private Formatter x;
public void openFile(String name){
    try {
        x = new Formatter(name);
    } catch (Exception e) {
        System.out.println("you have an error");
    }
}

public void addRecord(String input){
    x.format(input);
    x.format("%n", "pratiwi");
}

public void close(){
    x.close();

}

但是当我运行这个程序时,我有这样的错误:

Exception in thread "AWT-EventQueue-0" java.util.UnknownFormatConversionException: Conversion = 'm'
at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2691)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2720)
at java.util.Formatter.parse(Formatter.java:2560)
at java.util.Formatter.format(Formatter.java:2501)
at java.util.Formatter.format(Formatter.java:2455)
at program.CreatingFile.addRecord(CreatingFile.java:25)
at program.TUtama.writeFile(TUtama.java:758)

任何人都可以帮助我的代码?感谢&#39; S

1 个答案:

答案 0 :(得分:0)

您的文件包含“%m”,导致异常(“52.01%mengungguli”)。你可以用这个简单的例子来试试:

    Formatter f = new Formatter("datatraining.txt");
    f.format("%m");
    f.format("%n", "pratiwi");
    f.close();

输出:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'm'
    at java.util.Formatter$FormatSpecifier.conversion(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 Test.main(Test.java:14)

如果%符号不是格式说明符

,则必须确保转义%符号