java.util.scanner

I run into an error when I'm running this function. It finds a word in a certain file using Scanner.

Here it is:

public static boolean VerifyExistWord(File FileToSearch, String WordToFind) {

    boolean result = false;
    try (Scanner ind = new Scanner(FileToSearch)) {
        while (ind.hasNextLine()) {
            String word = ind.next();
            if (word.equalsIgnoreCase(WordToFind)) {
                result = true;
            }
        }
    } catch (FileNotFoundException e) {
    }
    return result;
}

But when I execute this, I run into a NoSuchElementException .

Exception in thread "pool-1-thread-13777" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:907) at java.util.Scanner.next(Scanner.java:1416) at TP.FuncoesAuxiliares.VerifyExistWord(FuncoesAuxiliares.java:66) at TP.PesquisaThreaded.run(PesquisaThreaded.java:90) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

Can someone help me?

Thanks.


确保首先有一个词可以调用next()

if(ind.hasNext()) {
    String word = ind.next();
    ...
}
链接地址: http://www.djcxy.com/p/96082.html

上一篇: 文件分类

下一篇: java.util.Scanner中