Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java扫描器是否有下一行NoTouchElementException?_Java - Fatal编程技术网

Java扫描器是否有下一行NoTouchElementException?

Java扫描器是否有下一行NoTouchElementException?,java,Java,我试图逐行读取一个大型csv文件,以便找到其中字符串的出现次数 下面是执行此操作的代码: public int getOffset(File file, String searched) throws FileNotFoundException { Scanner scanner = new Scanner(file).useDelimiter(System.getProperty("line.separator")); int occurences = 0; while

我试图逐行读取一个大型csv文件,以便找到其中字符串的出现次数

下面是执行此操作的代码:

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;
}
但是,在读取文件的最后一行后,它会再次检查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)

为什么它没有检测到它是文件的结尾?

使用
String s=scanner.nextLine()
而不是
String s=scanner.next()

这意味着您的代码如下所示:

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

通常,当使用
扫描仪时,您的
具有…
条件需要与
下一个…
数据检索方法相匹配,您正在检查下一行是否存在并扫描下一个单词。将while中的条件更改为
while(scanner.hasNext())
,或将扫描行更改为
String s=scanner.nextLine()

试试这个:

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

while(scanner.hasNextLine()){//检查下一行。String s=scanner.next();//读取分隔字符串。您必须保持一致,即使用hasNext、scanner.next()或hasNextLine()、scanner.nextLine()
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;
}