即使扫描仪不再具有要提供的下一个元素,您似乎仍在呼叫下一步……引发异常。
while(!file.next().equals(treasure)){ file.next(); }应该是这样的
boolean foundTreasure = false;while(file.hasNext()){ if(file.next().equals(treasure)){ foundTreasure = true; break; // found treasure, if you need to use it, assign to variable beforehand }} // out here, either we never found treasure at all, or the last element we looked as was treasure... act accordingly


