简体   繁体   English

为什么我看不到我的文本文件?

[英]Why can't I read my text file?

I am trying to read in a text file, but I keep getting "java.util.InputMismatchException". 我正在尝试读取文本文件,但是我不断收到“ java.util.InputMismatchException”。

Main class: 主班:

dc.loadData(new File("DVDCollection.txt"));

Class that reads/loads the files data: 读取/加载文件数据的类:

Scanner inputFile = null;
public void loadData(File fileName) {
    try {
        inputFile = new Scanner (new File("DVDCollection.txt"));
    }

    catch (Exception FileNotFoundException) {
        System.out.println("ERROR: " + FileNotFoundException.getMessage());
        System.exit(1);
    }

    while (inputFile.hasNext()) {

        String fileTitle = inputFile.nextLine();
        String fileCategory = inputFile.nextLine();
        String fileRunningTime = inputFile.nextLine();
        int fileYear = inputFile.nextInt();
        double filePrice = inputFile.nextDouble();

        DVD dvdEntry = new DVD(fileTitle, fileCategory, fileRunningTime, fileYear, filePrice);
        DVDlist.add(dvdEntry);
    }
    inputFile.close();
}

The text file has one piece of information per line, with no line breaks. 文本文件每行只有一条信息,没有换行符。

Example: 例:

Movie title1 (Sttring)
movie category1 (String)
movie duration1 (String)
movie year1 (int)
movie price1 (double)
Movie title2
movie category2
movie duration2
movie year2
movie price2
etc
etc...

And here is an actual example of what it's supposed to read in: 这是应该读入的实际示例:

Adam
Documentary
78 minutes
2012
7.99
Choo Choo
Documentary
60 minutes
2006
11.99

What the error says: 错误提示:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at XXXX_XXXX.DVDCollection.loadData(DVDCollection.java:29)
at XXXX_XXXX.DVDApplication.main(DVDApplication.java:14)

You probably have an issue with those lines: 您可能对这些行有疑问:

int fileYear = inputFile.nextInt();
double filePrice = inputFile.nextDouble();

At some point the expected type is not found (not a double). 在某些时候,找不到预期的类型(不是双精度型)。 Make sure you go to the next line and that your file contains no errors (numbers are formatted as expected...). 确保您转到下一行,并且您的文件中没有错误(数字按预期格式设置...)。

The InputMisMatchException (when thrown from nextDouble() ), indicates that "the next token does not match the Float regular expression, or is out of range" (quote from Javadoc ). InputMisMatchException (从nextDouble()抛出时)指示“下一个标记与Float正则表达式不匹配,或者超出范围”(引用自Javadoc )。

So the problem is most likely in the file you're trying to read. 因此,问题很可能出在您尝试读取的文件中。 Without its contents, however, it's hard to guess what exactly is the culprit. 但是,如果没有其内容,很难猜测出到底是什么原因。

Scanner#nextDouble() throws InputMismatchException - if the next token does not match the Float regular expression, or is out of range. Scanner#nextDouble()抛出InputMismatchException如果下一个标记与Float正则表达式不匹配或超出范围。

So clearly the input is not a float literal. 因此很明显输入不是浮点文字。 I am expecting there might be space, which makes double as string .. 我预计可能会有空间,这会使string double ..

From your trace shows that the Scanner has tried to read a value that is supposed to be double value is not a double value. 从您的跟踪显示, Scanner已尝试读取应该是double值的值,而不是double值。

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
**at java.util.Scanner.nextDouble(Unknown Source)**
at XXXX_XXXX.DVDCollection.loadData(DVDCollection.java:29)
at XXXX_XXXX.DVDApplication.main(DVDApplication.java:14)

In your case it will suit 在您的情况下,它将适合

String fileTitle = inputFile.nextLine();
String fileCategory = inputFile.nextLine();
String fileRunningTime = inputFile.nextLine();
int fileYear = Integer.parseInt(inputFile.nextLine());
double filePrice = Double.parseDouble(inputFile.nextLine());

I found the reason for your problem. 我找到了您问题的原因。

When you use nextline() it advances the scanner past the current line and places it at the beginning of next line. 当您使用nextline()时,它将使扫描仪前进到当前行的上方,并将其放置在下一行的开头。

But when you use nextInt or nextDouble then the scanner is advanced just past the nextInteger and next double value and placed at the end of the current line itself. 但是,当您使用nextInt或nextDouble时,扫描仪将前进到nextInteger和next double值之后,并放置在当前行的末尾。

Hence a \\n is also read as part of the next nextLine breaking the sequence. 因此,\\ n也会作为下一个破坏序列的nextLine的一部分被读取。

You can code like this as a work around. 您可以像这样解决该问题。

while (inputFile.hasNext()) {
            String fileTitle = inputFile.nextLine();
            String fileCategory = inputFile.nextLine();
            String fileRunningTime = inputFile.nextLine();
            int fileYear = Integer.parseInt(inputFile.nextLine());
            double filePrice = Double.parseDouble(inputFile.nextLine());
        }
        inputFile.close();
Just add the line marked with ***
while (inputFile.hasNext()) {

    String fileTitle = inputFile.nextLine();
    String fileCategory = inputFile.nextLine();
    String fileRunningTime = inputFile.nextLine();
    int fileYear = inputFile.nextInt();
    double filePrice = inputFile.nextDouble();

    DVD dvdEntry = new DVD(fileTitle, fileCategory, fileRunningTime,
        fileYear, filePrice);
    DVDlist.add(dvdEntry);

    if (inputFile.hasNext()) //***
        inputFile.nextLine();
}

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

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