我正试图从文件中逐字逐句地将每个文件首先添加到字典中,然后将所有文件添加为文档列表,但我开始出现此错误
once
调试时一切正常,并且没有显示错误,但在运行时会给出:
File files[] = directory.listFiles();
ArrayList<String> tockenList = new ArrayList<String>();
for (File f : files) {
Scanner s = null;
int DocID = Integer.parseInt(f.getName().replaceAll("\\D+", ""));
try {
s = new Scanner(f).useDelimiter("[ ,!?. ]+");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Doc doc = new Doc();
while (s.hasNext()) {
tockenList.add(s.next());
String str = s.next(); // this line is 34
Dictionary.getInstance().addElementToListOfTerms(str, DocID);
}
doc.addListOfTokens(DocID, tockenList);
s.close();
谢谢你们,我已经知道了。它应该是这样的
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at TeamLab.Main.main(Main.java:34)
答案 0 :(得分:0)
为什么不交换这些行
tockenList.add(s.next());
String str = s.next(); // this line is 34
是
String str = s.next();
tockenList.add(str);
当你正在测试hasNext
但是再读两次