繁体   English   中英

线程“ main”中的异常java.util.UnknownFormatConversionException:Conversion ='ti'

[英]Exception in thread “main” java.util.UnknownFormatConversionException: Conversion = 'ti'

package chapterreader;


import java.util.Scanner;
import java.io.File;

public class ChapterReader {

    public static void main(String[] args) throws Exception {
        Chapter myChapter = new Chapter();
        File chapterFile = new File("toc.txt");
        Scanner chapterScanner;

        //check to see if the file exists to read the data
        if (chapterFile.exists()) {
            System.out.printf("%7Chapter %14Title %69Page %80Length");

            chapterScanner = new Scanner(chapterFile);
            //Set Delimiter as ';' & 'new line'
            chapterScanner.useDelimiter(";|\r\n");
            while (chapterScanner.hasNext()) {
                //Reads all the data from file and set it to the object Chapter
                myChapter.setChapterNumber(chapterScanner.nextInt());
                myChapter.setChapterTitle(chapterScanner.next());
                myChapter.setStartingPageNumber(chapterScanner.nextInt());
                myChapter.setEndingPageNumber(chapterScanner.nextInt());
                displayProduct(myChapter);

            }
            chapterScanner.close();
        } else {
            System.out.println("Missing Chapter File");

        }

    }

    //Display the Chapter Information in a correct Format
    public static void displayProduct(Chapter reportProduct) {

        System.out.printf("%7d", reportProduct.getChapterNumber());
        System.out.printf("%-60s", reportProduct.getChapterTitle());
        System.out.printf("%-6d", reportProduct.getStartingPageNumber());
        System.out.printf("%-7d%n", reportProduct.getEndingPageNumber());
    }
}

但是然后我得到一个错误:

运行:线程“主”中的异常java.util.UnknownFormatConversionException:java.util.Formatter $ FormatSpecifier.checkDateTime(Formatter.java:2915)处的Conversion ='ti'。java.util.Formatter $ FormatSpecifier。(Formatter.java: 2678)在java.util.Formatter.parse(Formatter.java:2528)在java.io.PrintStream.format(PrintStream.java:970)在java.util.Formatter.format(Formatter.java:2469)在java。 io.PrintStream.printf(PrintStream.java:871)位于Chapterreader.ChapterReader.main(ChapterReader.java:17)上Java结果:1个成功建立(总时间:0秒)

这个错误怎么了? 请帮忙!

您的以下声明不可格式化。 那就是为什么它抛出UnknownFormatConversionException

 System.out.printf("%7Chapter %14Title %69Page %80Length");

如果您想分隔这些单词而不是使用以下方式

System.out.printf("%7s %14s %69s %80s", "Chapter", "Title", "Page", "Length");

代替

System.out.printf("%7Chapter %14Title %69Page %80Length");

我想你想要类似的东西

System.out.printf("%7s %14s %69s %80s%n", "Chapter", "Title", "Page",
        "Length");

并且您的消息告诉您格式String无效( %14Ti )。 Formatter#syntax Javadoc说(部分)

't''T'日期/时间日期和时间转换字符的前缀。 请参阅日期/时间转换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM