简体   繁体   English

Java Scanner hasNextLine NoSuchElementException?

[英]Java Scanner hasNextLine NoSuchElementException?

I am trying to read a large csv file line by line in order to find the number of occurences of a string in it. 我试图逐行读取一个大的csv文件,以便找到其中的字符串出现次数。

Here is the code doing it : 这是执行它的代码:

public int getOffset(File file, String searched) throws FileNotFoundException {
    Scanner scanner = new Scanner(file).useDelimiter(System.getProperty("line.separator"));
    int occurences = 0;
    while (scanner.hasNextLine()) {
        String s = scanner.next();
        if (s.indexOf(searched) >= 0) {
            occurences++;
        }
    }
    return occurences;
}

However, after it has read the last line of the file, it checks one more time the while condition, and exits with this exception : 但是,在读取文件的最后一行后,它再次检查while条件,并退出此异常:

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at fr.sgcib.cva.mat.MatWriter.getOffset(MatWriter.java:83)
at fr.sgcib.cva.mat.MatWriter.writeFooter(MatWriter.java:71)
at fr.sgcib.cva.mat.NettingNodeHierarchyExtract.getLeNodes(NettingNodeHierarchyExtract.java:65)
at fr.sgcib.cva.mat.Mat.main(Mat.java:55)

Why doesn't it detect it's the end of the file ? 为什么不检测它是文件的结尾?

Use String s = scanner.nextLine(); 使用String s = scanner.nextLine(); instead of String s = scanner.next(); 而不是String s = scanner.next();

That means that your code would look like this: 这意味着您的代码将如下所示:

public int getOffset(File file, String searched) throws FileNotFoundException {
    Scanner scanner = new Scanner(file).useDelimiter(System.getProperty("line.separator"));
    int occurences = 0;
    while (scanner.hasNextLine()) {
        String s = scanner.nextLine();
        if (s.indexOf(searched) >= 0) {
            occurences++;
        }
    }
    return occurences;
}

In general, when using Scanner your has... condition needs to match the next... data retrieval method 通常,在使用Scanner你的has...条件需要匹配next...数据检索方法

You are checking for the existence of the next line and scanning the next word. 您正在检查下一行是否存在并扫描下一个单词。 Either change the condition in while to while(scanner.hasNext()) or the scan line to String s = scanner.nextLine() . 将while到while(scanner.hasNext())或扫描行中的条件更改为String s = scanner.nextLine()

Try this: 尝试这个:

public int getOffset(File file, String searched) throws FileNotFoundException {
    Scanner scanner = new Scanner(file).useDelimiter(System.getProperty("line.separator"));
    int occurences = 0;
    while (scanner.hasNext()) {
        String s = scanner.next();
        if (s.indexOf(searched) >= 0) {
            occurences++;
        }
    }
    return occurences;
}

or 要么

public int getOffset(File file, String searched) throws FileNotFoundException {
    Scanner scanner = new Scanner(file).useDelimiter(System.getProperty("line.separator"));
    int occurences = 0;
    while (scanner.hasNextLine()) {
        String s = scanner.nextLine();
        if (s.indexOf(searched) >= 0) {
            occurences++;
        }
    }
    return occurences;
}

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

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