【问题标题】:Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = 'ti'线程“主”java.util.UnknownFormatConversionException 中的异常:Conversion = 'ti'
【发布时间】:2015-04-15 02:50:41
【问题描述】:
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());
    }
}

然后我得到一个错误:

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

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

【问题讨论】:

    标签: java multithreading exception


    【解决方案1】:

    您的以下语句不可格式化。这就是为什么它抛出UnknownFormatConversionException

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

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

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

    【讨论】:

      【解决方案2】:

      代替

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 1970-01-01
        • 2018-06-06
        • 2016-07-22
        • 2013-02-17
        • 2013-12-20
        • 1970-01-01
        相关资源
        最近更新 更多