2011-11-07 127 views
7

因此,這裏是我的代碼:沒有這樣的元素異常?

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

出於某種原因,我得到一個沒有這樣的元素異常

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

+0

如果您可以使用註釋標記堆棧跟蹤中提到的代碼行,那麼我們可以得到一個參考點,這將會很好。 –

+0

你可以發佈文件內容嗎? – Tom

回答

12

它看起來像你調用next即使掃描儀不再有下一個元素,以提供...拋出異常。在while循環

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 
+0

我試過了你的代碼,但後來我得到了一個沒有這樣的元素異常爲我的最小/最大nextInt(),這很奇怪,因爲不等於它的循環等於寶?那之後不應該還有下一個int嗎?順便說一下,我在主帖中鏈接了txt文件,相關的文件是armor.txt和TreasureClassEx.txt。謝謝! – Akaraka

+0

我編輯我的迴應是更清楚一點。有可能你根本找不到寶藏......所以你對nextInt的調用會失敗,因爲沒有更多的令牌可供讀取。我會用上面的代碼進行測試,然後添加一個調試來查看file.next()。equals(treasure)是否被評估爲true。 – Bryan

0

看起來你file.next()行被拋NoSuchElementException異常,由於掃描儀達到了文件的末尾。閱讀下一個()Java API here

此外,你不應該在循環和while條件中調用next()。在while條件下,您應該檢查下一個標記是否可用,並在while循環內檢查它是否等於寶藏。

0

我知道這個問題3年前被aked,但我有同樣的問題,什麼解決它,而不是把:

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

我做了一個迭代的開始,然後檢查條件使用:

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

我希望這會幫助一些人在某個階段。

0

我在處理大型數據集時遇到了同樣的問題。我注意到的一件事是當掃描器達到endOfFile時拋出NoSuchElementException,它不會影響我們的數據。

在這裏,我把我的代碼放在try blockcatch block處理exception。如果您不想執行任何任務,也可以將其保留爲空。

對於上述問題,因爲你使用file.next()無論是在條件和while循環,你可以處理異常

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

這完美地工作對我來說,如果有任何一個角落的情況下爲我方法,請通過評論讓我知道。

+1

[空的catch塊通常是一個壞主意](https://stackoverflow.com/questions/1234343/why-are-empty-catch-blocks-a-bad-idea) –

相關問題