2016-11-14 101 views
4

我正在做一個口袋妖怪戰鬥模擬器,(基本上pokemonshowdown gen1),試圖自動化製作口袋妖怪數組,但運行到掃描儀問題。文件被格式化爲:可學習動作的Name.Type1.Type2.hp.attack.defense.special.speed.list。所以: Aerodactyl.Flying.Rock.80.105.65.60.130.Agility,Bide,Bite,雙邊,雙團隊,Dragon Rage,Fire Blast,飛,Hyper Beam,模仿,憤怒,剃刀風,反映,休息,天空襲擊,替補,超音速,迅捷,取下,有毒,翼部攻擊。從文件輸入填充數組

我已經得到了一個方法爲我的typeArray和moveArray工作,但由於某種原因使用基本上相同的循環掃描程序返回空的標記,而不是文件中的內容。

例外:

0 Exception in thread "main" java.lang.NumberFormatException: For input string: "" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 
at java.lang.Integer.parseInt(Integer.java:592) 
at java.lang.Integer.parseInt(Integer.java:615) 
at Controller.initPokemonArray(Controller.java:169) 
at Controller.<init>(Controller.java:29) 
at Driver.main(Driver.java:15) 

這裏的整個方法,它在parseInt函數調用惠普拋出的錯誤。

private Pokemon[] initPokemonArray() { 
    Pokemon[] pokemonArray = new Pokemon[83]; 
    try { 
     Scanner inputScan = new Scanner(new File("src/pokemon")).useDelimiter("."); 
     String name = ""; 
     Type type1 = typeArray[0]; 
     String inputType1 = ""; 
     Type type2 = typeArray[0]; 
     String inputType2 = ""; 
     int hp = 0; 
     int atk = 0; 
     int def = 0; 
     int spc = 0; 
     int spe = 0; 
     String[] lm = {}; 
     Move[] learnableMoves; 
     int counter = 0; 
     while (counter < 83) { 
     System.out.print(counter); 
      if (inputScan.hasNextLine()) { 
       name = inputScan.next(); 
       System.out.println(name+" "); 
       //System.out.print("name"); 
       inputType1 = inputScan.next(); 
       for (int i = 0;i < 16;i++) 
        if (inputType1.equals(typeArray[i].toString())) 
         type1 = typeArray[i]; 
       System.out.println(type1.toString()+" "); 
       inputType2 = inputScan.next(); 
       for (int i = 0;i < 16;i++) 
        if (inputType2.equals(typeArray[i].toString())) 
         type2 = typeArray[i]; 
       System.out.println(type2.toString()+" "); 
       hp = Integer.parseInt(inputScan.next()); 
       System.out.println(hp+" "); 
       atk = Integer.parseInt(inputScan.next()); 
       System.out.println(atk+" "); 
       def = Integer.parseInt(inputScan.next()); 
       System.out.println(def+" "); 
       spc = Integer.parseInt(inputScan.next()); 
       System.out.println(spc+" "); 
       spe = Integer.parseInt(inputScan.next()); 
       System.out.println(spe+" "); 
       lm = inputScan.next().split(","); 
       System.out.println(); 
      } 
      //TODO move this to private helper method 
      learnableMoves = new Move[lm.length]; 
      for (int i = 0;i < 160;i++) { 
       for (int j = 0;j < lm.length;j++) { 
        if (lm[j] == moveArray[i].getName()) 
         learnableMoves[j] = moveArray[i]; 
       } 
      } 
      pokemonArray[counter] = new Pokemon(name,type1,type2,hp,atk,def,spc,spe,learnableMoves); 
      counter++; 
     } 
     inputScan.close(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return pokemonArray; 
} 

免責聲明:這是我的Java 2場一期工程,也是我在這裏的第一篇文章,所以我不知道到底是什麼我應該這樣做,只是讓它在這裏知道了。

+0

它拋出了什麼錯誤? – DejaVuSansMono

+1

「parseInt調用hp時出錯」 - 什麼樣的錯誤?它是否發生在每條生產線或特別的一些生產線上?如果你包含導致問題的部分'src/pokemon'會很有幫助。 – bradimus

+0

只要你有一個具體的問題,並不只是尋找別人來寫,如果對你來說你很好。嘗試先將inputScan.next()保存到字符串變量中並打印出來。你可能正在拿起一個空間或其他東西。另請參閱inputScan.nextInt()。 – Joe

回答