簡體   English   中英

JAVA-掃描儀-錯誤NoSuchElementException

[英]JAVA - Scanner - Error NoSuchElementException

運行此函數時遇到錯誤。 它使用掃描儀在特定文件中查找單詞。

這里是:

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

但是,當我執行此操作時,我遇到了NoSuchElementException

TP處的java.util.Scanner.next(Scanner.java:1416)處的java.util.Scanner.throwFor(Scanner.java:907)處的線程“ pool-1-thread-13777”中的java.util.NoSuchElementException。 TP上的FuncoesAuxiliares.VerifyExistWord(FuncoesAuxiliares.java:66)在Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)上的TP.PesquisaThreaded.run(PesquisaThreaded.java:90)在java.util.concurrent.ThreadPerExecutor $ .run(ThreadPoolExecutor.java:615)在java.lang.Thread.run(Thread.java:745)

有人能幫我嗎?

謝謝。

確保首先有一個單詞要調用next()

if(ind.hasNext()) {
    String word = ind.next();
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM