2015-09-21 197 views
0

我在運行此函數時遇到錯誤。它使用掃描儀在某個文件中找到一個字。JAVA - 掃描儀 - 錯誤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

在線程異常 「池-1-線程13777」 在 java.util.Scanner.next在 java.util.Scanner.throwFor(Scanner.java:907) java.util.NoSuchElementException( Scanner.java:1416)在 TP.FuncoesAuxiliares.VerifyExistWord(FuncoesAuxiliares.java:66) 在TP.PesquisaThreaded.run(PesquisaThreaded.java:90)在 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)

有人可以幫我嗎?

謝謝。

+0

你真的有編譯器錯誤?如果沒有,請刪除此標籤。 –

+0

好的!你有建議來解決這個問題? – fipcurren88

+1

你正在檢查'hasNextLine()' - 如果你調用'nextLine()'而不是'next()',它可能會工作嗎? –

回答

2

確保有一個調用next()上第一:

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