因此,我有以下代碼來打開兩個文件。 filew2v包含每個單詞後面帶有換行符的單詞。現在我要檢查,如果使用下面的代碼在fileverb存在filew2v的話:使用掃描器檢查file1中是否存在file1中的字NoSuchElementException
public class CompareWords {
public void CompareWordsOK(File fileverb, File filew2v) throws IOException {
Scanner scannerw2v = new Scanner(filew2v);
Scanner scannerverb = new Scanner(fileverb);
while (scannerw2v.hasNextLine()) {
String nextToken1 = scannerw2v.next();
while (scannerverb.hasNextLine()) {
String nextToken = scannerverb.next();
if (nextToken.equalsIgnoreCase(nextToken1)) {
System.out.print("EXIST");
}
}
}
}
}
我打電話給我的方法使用:
compverb.CompareVerbOK(new File("/home/user/Desktop/listOfwords.txt"), new File("/home/user/Desktop/listofwordsforchecking.txt"));
我收到以下錯誤:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at CompareVerb.CompareVerbOK(CompareVerb.java:13)
at Main.main(Main.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147
有人可以指點我正確的方向來尋找我的錯誤嗎?