线程" main"中的例外情况java.util.UnknownFormatConversionException:Conversion =' ti'

时间:2015-04-15 02:50:42

标签: java multithreading exception

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

但后来我得到了一个错误:

  

run:线程中的异常" main"   java.util.UnknownFormatConversionException:Conversion =' ti'在   java.util.Formatter中的$ FormatSpecifier.checkDateTime(Formatter.java:2915)     at java.util.Formatter $ FormatSpecifier。(Formatter.java:2678)     在java.util.Formatter.parse(Formatter.java:2528)at   java.util.Formatter.format(Formatter.java:2469)at   java.io.PrintStream.format(PrintStream.java:970)at   java.io.PrintStream.printf(PrintStream.java:871)at   chapterreader.ChapterReader.main(ChapterReader.java:17)Java结果:1   建立成功(总时间:0秒)

这个错误有什么问题?请帮忙!

2 个答案:

答案 0 :(得分:1)

您的以下声明不支持格式化。这就是它抛出UnknownFormatConversionException

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

如果你想分开这些词而不是使用以下方式

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

答案 1 :(得分:1)

而不是

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

我觉得你想要像

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

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

  

't''T'日期/时间日期和时间转换字符的前缀。见Date/Time Conversions