[英]java.util.NoSuchElementException - Exception in thread “main” error
I'm not too sure about this error as I cannot figure out what's going on. 我不太确定这个错误,因为我无法弄清楚发生了什么。 Through the debugger, I can see that the file is been successfully read, but on the last index, I get an error.
通过调试器,我可以看到文件已成功读取,但是在最后一个索引上,我得到了一个错误。 Anyone know why?
有人知道为什么吗?
try {
txtin = new Scanner(gameFile);
//String line;
while(txtin.hasNext()) {
for(int i = 0; i < 15; i++) {
Grid[i] = txtin.next();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if (txtin != null)
txtin.close();
}
The error I get is: 我得到的错误是:
Exception in thread "main" java.util.NoSuchElementException
How do I fix this? 我该如何解决?
Every time you call Iterator#next()
, it moves the underlying cursor forward. 每次调用
Iterator#next()
,它将向前移动基础光标。 You are calling txtin.next()
in a for loop for 15 times. 您正在for循环中调用
txtin.next()
15次。 There may be a case where there are less than 15 token and the cursor went to a place that doesn't have such element Thus NoSuchElementException
可能存在少于15个令牌的情况,并且光标移动到没有此类元素的位置,因此
NoSuchElementException
Replace 更换
while(txtin.hasNext()) {
for(int i = 0; i < 15; i++) {
Grid[i] = txtin.next();
}
}
with 同
int i=0;
while(txtin.hasNext()) {
Grid[i++] = txtin.next();
}
For the reasons described by Gagan Chouhan. 由于Gagan Chouhan描述的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.