没有这样的元素例外?

IT小君   2021-12-09T03:40:20

所以这是我的代码:

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
    }
 }

出于某种原因,我收到了 No such Element Exception

    at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at LootGenerator.getArmor(LootGenerator.java:43)
at LootGenerator.getTreasureClass(LootGenerator.java:68)
at LootGenerator.getMonster(LootGenerator.java:127)
at LootGenerator.theGame(LootGenerator.java:19)
at LootGenerator.main(LootGenerator.java:11)

我不知道为什么。基本上我的程序正在搜索两个文本文件 - Armor.txt 和 TreasureClassEx.txt。getTreasureClass 从怪物那里接收一个宝藏类并搜索 txt 直到它到达一个基础盔甲项目(一个不以 tc: 开头的字符串)然后它搜索 getArmor 以寻找与它获得的基础盔甲名称相匹配的盔甲宝物类。任何意见,将不胜感激!谢谢!

txt 文件的链接在这里:http : //www.cis.upenn.edu/~cis110/hw/hw06/large_data.zip

点击广告,支持我们为你提供更好的服务
评论(5)
IT小君

即使扫描仪不再提供下一个元素,您似乎也在调用 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
2021-12-09T03:40:22   回复
IT小君

我在处理大型数据集时遇到了同样的问题。我注意到的一件事NoSuchElementException是当 Scanner 到达 时抛出endOfFile,它不会影响我们的数据。

在这里,我已将代码放入try blockcatch block处理exception. 如果您不想执行任何任务,也可以将其留空。

对于上述问题,因为您file.next()在条件和 while 循环中都使用,所以您可以将异常处理为

while(!file.next().equals(treasure)){
    try{
        file.next(); //stack trace error here
       }catch(NoSuchElementException e) {  }
}

这对我来说非常有效,如果我的方法有任何极端情况,请通过评论告诉我。

2021-12-09T03:40:23   回复
IT小君

出现同样问题的另一种情况, map.entrySet().iterator().next()

如果 Map 对象中没有元素,那么上面的代码将返回NoSuchElementException. 一定hasNext()要先打电话

2021-12-09T03:40:23   回复
IT小君

看起来你的 file.next() 行在 while 循环中抛出 NoSuchElementException ,因为扫描仪到达文件末尾。这里阅读 next() java API

此外,您不应在循环和 while 条件中调用 next() 。在 while 条件下,您应该检查下一个令牌是否可用,并在 while 循环内检查它是否等于宝藏。

2021-12-09T03:40:24   回复
IT小君

我知道这个问题是 3 年前提出的,但我遇到了同样的问题,解决它的方法是:

 while (i.hasNext()) {
    // code goes here 
}

我在开始时进行了一次迭代,然后使用以下方法检查条件:

do {
   // code goes here
} while (i.hasNext());

我希望这会在某个阶段对某些人有所帮助。

2021-12-09T03:40:24   回复