gpt4 book ai didi

JAVA - 扫描仪 - 错误 NoSuchElementException

转载 作者:行者123 更新时间:2023-12-02 04:21:39 27 4
gpt4 key购买 nike

我在运行此函数时遇到错误。它使用扫描仪在某个文件中查找单词。

这里是:

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

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)

有人可以帮助我吗?

谢谢。

最佳答案

确保有一个单词首先调用next():

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

关于JAVA - 扫描仪 - 错误 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com