Java 没有这样的元素例外吗?
这是我的代码:Java 没有这样的元素例外吗?,java,exception,Java,Exception,这是我的代码: public static void getArmor(String treasure) throws FileNotFoundException{ Random rand=new Random(); Scanner file=new Scanner(new File ("armor.txt")); while(!file.next().equals(treasure)){ file.next(); //stack trace er
public static void getArmor(String treasure)
throws FileNotFoundException{
Random rand=new Random();
Scanner file=new Scanner(new File ("armor.txt"));
while(!file.next().equals(treasure)){
file.next(); //stack trace error here
}
int min=file.nextInt();
int max=file.nextInt();
int defense=min + (int)(Math.random() * ((max - min) + 1));
treasure=treasure.replace("_", " ");
System.out.println(treasure);
System.out.println("Defense: "+defense);
System.out.println("=====");
System.out.println();
}
public static void getTreasureClass(Monster monGet)
throws FileNotFoundException{
Random rand = new Random();
String tc=monGet.getTreasureClass();
while (tc.startsWith("tc:")){
Scanner scan=new Scanner(new File ("TreasureClassEx.txt"));
String eachLine=scan.nextLine();
while(!tc.equals(scan.next())){
eachLine=scan.nextLine();
}
for (int i=0;i<=rand.nextInt(3);i++){
tc=scan.next();
}
getArmor(tc); //stack trace error here
}
}
我不知道为什么。基本上,我的程序正在搜索两个文本文件——armor.txt和treaseClassex.txt。getTreasureClass从怪物那里接收到一个宝藏类,并在txt中搜索,直到它到达一个基础护甲项目(一个字符串,不以tc:开头)。然后,它在getArmor中搜索与宝藏类中获得的基础护甲名称匹配的护甲。任何建议都将不胜感激!谢谢
指向txt文件的链接在这里:看起来您正在调用next,即使扫描仪不再有下一个要提供的元素。。。正在抛出异常
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
看起来像是您的file.next()行在while循环中抛出了NoSuchElementException,因为扫描仪到达了文件的末尾。阅读next()java API
此外,您不应该在循环和while条件中调用next()。在while条件下,您应该检查下一个令牌是否可用,并在while循环内检查它是否等于宝藏。我知道这个问题是3年前提出的,但我刚刚遇到了同样的问题,解决它的方法不是放:
while (i.hasNext()) {
// code goes here
}
我在开始时进行了一次迭代,然后使用以下方法检查条件:
do {
// code goes here
} while (i.hasNext());
我希望这将在某些阶段帮助一些人。我在处理大型数据集时遇到了同样的问题。我注意到的一件事是当扫描器到达
结束文件时抛出NoTouchElementException
,它不会影响我们的数据
在这里,我将代码放在try block
中,catch block
处理异常。如果不想执行任何任务,也可以将其留空
对于上面的问题,由于您在条件和while循环中都使用了file.next()
,因此可以按如下方式处理异常:
while(!file.next().equals(treasure)){
try{
file.next(); //stack trace error here
}catch(NoSuchElementException e) { }
}
这对我来说非常有效,如果我的方法有任何极端情况,请通过评论让我知道 出现同样问题的另一种情况,
map.entrySet().iterator().next()
如果Map对象中没有元素,则上述代码将返回NoTouchElementException
。请确保首先调用hasNext()
。如果您可以使用注释标记堆栈跟踪中提到的代码行,这样我们就可以获得参考点,那就太好了。您可以发布文件内容吗?我为此尝试了您的代码,但后来我的min/max nextInt()没有出现这样的元素异常,这很奇怪,因为当它等于宝藏时,循环不就破裂了吗?在那之后不应该还有下一个int吗?顺便说一下,我在我的主要帖子中链接了txt文件,相关的是armor.txt和treaseClassex.txt。谢谢我对我的回答进行了编辑,使之更加清晰。有可能你根本找不到宝藏。。。因此,您对nextInt的调用在此将失败,因为没有更多的令牌可读取。我将使用类似于上述代码的代码进行测试,并添加一个调试,以查看file.next().equals(treasure)是否被计算为true。从while切换到do..虽然通常无法避免异常,但如果对next()的第一次调用失败,则会导致异常。
while(!file.next().equals(treasure)){
try{
file.next(); //stack trace error here
}catch(NoSuchElementException e) { }
}