2016-11-14 198 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

回答