2012-02-14 104 views
-1

線程「main」中的異常java.lang.NumberFormatException:對於輸入字符串:「npcId」 當我嘗試運行它時。我的代碼有什麼問題?在線程「main」java.lang.NumberFormatException中的異常:對於輸入字符串:「npcId」

出了什麼問題? 該類所做的幾乎是將NPC的unpackack定義讀取並打包到一個文件中,但它沒有正確打包。 (請參見下面的格式,我使用)

類:

package com.rs.utils; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.ByteBuffer; 
import java.nio.channels.FileChannel; 
import java.util.HashMap; 

import com.rs.game.npc.combat.NPCCombatDefinitions; 

// Referenced classes of package com.rs.utils: 
//   Logger 

public final class NPCCombatDefinitionsL 
{ 

    public static void init() 
    { 
     if((new File("data/npcs/packedCombatDefinitions.ncd")).exists()) 
      loadPackedNPCCombatDefinitions(); 
     else 
      loadUnpackedNPCCombatDefinitions(); 
    } 

    public static NPCCombatDefinitions getNPCCombatDefinitions(int npcId) 
    { 
     NPCCombatDefinitions def = (NPCCombatDefinitions)npcCombatDefinitions.get(Integer.valueOf(npcId)); 
     if(def == null) 
      return DEFAULT_DEFINITION; 
     else 
      return def; 
    } 

    private static void loadUnpackedNPCCombatDefinitions() 
    { 
     int count = 0; 
     Logger.log("NPCCombatDefinitionsL", "Packing npc combat definitions..."); 
     try 
     { 
      DataOutputStream out = new DataOutputStream(new FileOutputStream("data/npcs/packedCombatDefinitions.ncd")); 
      BufferedReader in = new BufferedReader(new FileReader("data/npcs/unpackedCombatDefinitionsList.txt")); 
      do 
      { 
       String line = in.readLine(); 
       count++; 
       if(line == null) 
        break; 
       if(!line.startsWith("//")) 
       { 
        String splitedLine[] = line.split(" - ", 2); 
        if(splitedLine.length != 2) 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString()); 
        int npcId = Integer.parseInt(splitedLine[0]); 
        String splitedLine2[] = splitedLine[1].split(" ", 12); 
        if(splitedLine2.length != 12) 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString()); 
        int hitpoints = Integer.parseInt(splitedLine2[0]); 
        int attackAnim = Integer.parseInt(splitedLine2[1]); 
        int defenceAnim = Integer.parseInt(splitedLine2[2]); 
        int deathAnim = Integer.parseInt(splitedLine2[3]); 
        int attackDelay = Integer.parseInt(splitedLine2[4]); 
        int deathDelay = Integer.parseInt(splitedLine2[5]); 
        int respawnDelay = Integer.parseInt(splitedLine2[6]); 
        int maxHit = Integer.parseInt(splitedLine2[7]); 
        int attackStyle; 
        if(splitedLine2[8].equalsIgnoreCase("MELEE")) 
         attackStyle = 0; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("RANGE")) 
         attackStyle = 1; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("MAGE")) 
         attackStyle = 2; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("SPECIAL")) 
         attackStyle = 3; 
        else 
        if(splitedLine2[8].equalsIgnoreCase("SPECIAL2")) 
         attackStyle = 4; 
        else 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString()); 
        int attackGfx = Integer.parseInt(splitedLine2[9]); 
        int attackProjectile = Integer.parseInt(splitedLine2[10]); 
        int agressivenessType; 
        if(splitedLine2[11].equalsIgnoreCase("PASSIVE")) 
         agressivenessType = 0; 
        else 
        if(splitedLine2[11].equalsIgnoreCase("AGRESSIVE")) 
         agressivenessType = 1; 
        else 
         throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString()); 
        out.writeShort(npcId); 
        out.writeShort(hitpoints); 
        out.writeShort(attackAnim); 
        out.writeShort(defenceAnim); 
        out.writeShort(deathAnim); 
        out.writeByte(attackDelay); 
        out.writeByte(deathDelay); 
        out.writeInt(respawnDelay); 
        out.writeShort(maxHit); 
        out.writeByte(attackStyle); 
        out.writeShort(attackGfx); 
        out.writeShort(attackProjectile); 
        out.writeByte(agressivenessType); 
        npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType)); 
       } 
      } while(true); 
      in.close(); 
      out.close(); 
     } 
     catch(FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private static void loadPackedNPCCombatDefinitions() 
    { 
     try 
     { 
      RandomAccessFile in = new RandomAccessFile("data/npcs/packedCombatDefinitions.ncd", "r"); 
      FileChannel channel = in.getChannel(); 
      int npcId; 
      int hitpoints; 
      int attackAnim; 
      int defenceAnim; 
      int deathAnim; 
      int attackDelay; 
      int deathDelay; 
      int respawnDelay; 
      int maxHit; 
      int attackStyle; 
      int attackGfx; 
      int attackProjectile; 
      int agressivenessType; 
      for(ByteBuffer buffer = channel.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0L, channel.size()); buffer.hasRemaining(); npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType))) 
      { 
       npcId = buffer.getShort() & 0xffff; 
       hitpoints = buffer.getShort() & 0xffff; 
       attackAnim = buffer.getShort() & 0xffff; 
       defenceAnim = buffer.getShort() & 0xffff; 
       deathAnim = buffer.getShort() & 0xffff; 
       attackDelay = buffer.get() & 0xff; 
       deathDelay = buffer.get() & 0xff; 
       respawnDelay = buffer.getInt(); 
       maxHit = buffer.getShort() & 0xffff; 
       attackStyle = buffer.get() & 0xff; 
       attackGfx = buffer.getShort() & 0xffff; 
       attackProjectile = buffer.getShort() & 0xffff; 
       agressivenessType = buffer.get() & 0xff; 
      } 

      channel.close(); 
      in.close(); 
     } 
     catch(IOException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private NPCCombatDefinitionsL() 
    { 
    } 

    private static final HashMap<Integer, NPCCombatDefinitions> npcCombatDefinitions = new HashMap<Integer, NPCCombatDefinitions>(); 
    private static final NPCCombatDefinitions DEFAULT_DEFINITION = new NPCCombatDefinitions(1, -1, -1, -1, 5, 1, 1, 0, 0, -1, -1, 0); 
    @SuppressWarnings("unused") 
    private static final String PACKED_PATH = "data/npcs/packedCombatDefinitions.ncd"; 
} 

文本文件格式: http://pastebin.com/ngrECkuD

+1

不具有你去這裏,亞歷克斯,但該變量,正確的說法是'splitLine' 。動詞「split」的過去式實際上是「拆分」,如「我昨天分割日誌」。你似乎試圖使用不是英文單詞的「splitted」,並且在任何情況下,你都錯過了一個't',這將使得基本單詞「splite」,而不是英語中的一個。無論如何,與這個問題完全無關,我只是一個學生:-) – paxdiablo 2012-02-14 01:12:17

回答

1

因爲你正在讀,並試圖解析字符串npcId,而不是以字符串表示的關聯號碼 - 7344

你大概的意思是:代替

int npcId = Integer.parseInt(splitedLine[1]); 

int npcId = Integer.parseInt(splitedLine[0]); 

下一次,請寫你的問題時,請參考http://sscce.org/

+0

嗯,現在我得到這個 – 2012-02-14 01:12:01

+0

xception在線程「主」java.lang.RuntimeException:無效的NPC戰鬥定義行:1,npcId - 7344 – 2012-02-14 01:12:06

+0

@AlexDaSilva - 因爲你可能在你的輸入文件中有第14行,它有0個字段,而不是你期望的2個字段。時間回到上一頁,並理解你的代碼在進一步繼續之前正在做什麼...... – ziesemer 2012-02-14 01:14:44

0

您發佈的格式不正確。這是封隔器可以讀取的格式:

實施例: // npcId - 生命點數attackAnim defenceAnim deathAnim attackDelay deathDelay respawnDelay maxHit attackStyle attackGfx attackProjectile agressivenessType


//曼 - 2 < <此這裏是告訴你什麼是NPC。

1 - 70 422 425 836 10 1 60 10 MELEE -1 -1被動


相關問題