2012-02-14 4 views
-1

스레드 "main"의 예외 java.lang.NumberFormatException : 입력 문자열의 경우 : "npcId" 실행하려고하면 .내 코드에 어떤 문제가 있습니까? 스레드 "main"의 예외 java.lang.NumberFormatException : 입력 문자열의 경우 : "npcId"

무엇이 잘못 되었나요? 클래스가하는 일은 NPC의 unPacked 정의를 읽고 파일로 압축하지만 올바르게 패킹되지 않습니다.

클래스를 (형식 내가 사용 아래 참조) :

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"를 사용하려고 시도한 것 같습니다. 어쨌든 영어 단어가 아닌 "splite"라는 기본 단어를 만드는 "t"를 놓친 것입니다. 어쨌든, 질문에 전혀 관련이없는 나는 단지 방탕하고있다 :-) – paxdiablo

답변

1

당신이 읽고 문자열 npcId 구문 분석을 시도하고 있기 때문에, 관련 수는 String가 아니고 - 7344.

는 당신은 아마 의미 :

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

대신 : 질문을 작성할 때

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

다음에, http://sscce.org/를 참조하십시오.

+0

흠, 이제 이걸 –

+0

"main"스레드의 xception java.lang.RuntimeException : 유효하지 않은 NPC 전투 정의 라인 : 1, npcId - 7344 –

+0

@AlexDaSilva - 아마도 당신이 예상하는 2 대신에 0 개의 필드가있는 입력 파일에서 14 번 라인에 도달했기 때문입니다. 시간을 되돌려 놓고 앞으로 나아 가기 전에 코드의 각 라인이 무엇을하는지 이해하십시오 ... – ziesemer

0

게시 한 형식이 올바르지 않습니다. 체력 attackAnim defenceAnim deathAnim attackDelay deathDelay respawnDelay maxHit attackStyle attackGfx attackProjectile agressivenessType


// 남자 - - 2 < <이 // npcId :

예 :이 포장이 읽을 수있는 형식입니다 여기에 npc가 무엇인지 알려주는 것입니다.

1-70 422 425 836 5 1 60 10 명 -1 -1 PASSIVE


관련 문제