2012-08-03 121 views
0

我試圖從txt文件加載某些數據時出現此錯誤。嘗試加載文件時出現java.lang.NumberFormatException

public static boolean initalize(String FileName) { 
    String line = ""; 

    String token = ""; 

    String token2 = ""; 

    String token2_2 = ""; 

    String[] token3 = new String[10]; 

    boolean EndOfFile = false; 

    BufferedReader characterfile = null; 

    try { 

      characterfile = new BufferedReader(new FileReader("./Data/data/" 

          + FileName)); 

    } catch (FileNotFoundException fileex) { 
     Misc.println("File not loaded"); 

      return false; 

    } 

    try { 

      line = characterfile.readLine(); 

    } catch (IOException ioexception) { 


      return false; 

    } 

    while ((EndOfFile == false) && (line != null)) { 

      **line = line.trim(); 
      int spot = line.indexOf("="); 
      if (spot > -1) { 
        token = line.substring(0, spot); 
        token = token.trim(); 
        token2 = line.substring(spot + 1); 
        token2 = token2.trim(); 
        token2_2 = token2.replaceAll("\t\t", "\t"); 
        token2_2 = token2_2.replaceAll("\t\t", "\t"); 
        token2_2 = token2_2.replaceAll("\t\t", "\t"); 
        token2_2 = token2_2.replaceAll("\t\t", "\t"); 
        token2_2 = token2_2.replaceAll("\t\t", "\t"); 
        token3 = token2_2.split("\t");** 

        if (token.equals("drop")) { 

          int id = Integer.parseInt(token3[0]); 

          int x = Integer.parseInt(token3[1]); 

          int y = Integer.parseInt(token3[2]); 

          int amount = Integer.parseInt(token3[3]); 

          int height = Integer.parseInt(token3[4]); 
          globalDrops.add(new GlobalDrop(id,amount,x,y,height)); 

        } 

      } else { 

        if (line.equals("[ENDOFDROPLIST]")) { 

          try { 

            characterfile.close(); 

          } catch (IOException ioexception) { 

          } 

          return true; 

        } 

      } 

      try { 

        line = characterfile.readLine(); 

      } catch (IOException ioexception1) { 

        EndOfFile = true; 

      } 

    } 

    try { 

      characterfile.close(); 

    } catch (IOException ioexception) { 

    } 

    return false; 

} 

然而,它一直給我這個錯誤:

[8/3/12 5:24 PM]: Exception in thread "main" [8/3/12 5:24 PM]: java.lang.NumberFormatException: For input string: "1923  3208 3214 1  0  2  " 

這是文本文件是如何被格式化:

sque = 1923  3208 3214 1  0  2  Square 

這是爲什麼給我這個錯誤?這是否與/ t/split有關?

感謝

這是一個工作:

sque = 1923 3208 3214 1 0 2  Square 

不過,我試圖加載在這些400,這將是痛苦的改變所有這些一次

+0

您粘貼的版本以空格分隔。 – Wug 2012-08-03 21:32:42

回答

0

更換這一切

   token = line.substring(0, spot); 
       token = token.trim(); 
       token2 = line.substring(spot + 1); 
       token2 = token2.trim(); 
       token2_2 = token2.replaceAll("\t\t", "\t"); 
       token2_2 = token2_2.replaceAll("\t\t", "\t"); 
       token2_2 = token2_2.replaceAll("\t\t", "\t"); 
       token2_2 = token2_2.replaceAll("\t\t", "\t"); 
       token2_2 = token2_2.replaceAll("\t\t", "\t"); 
       token3 = token2_2.split("\t");** 

String[] cmd = line.split("="); 
token = cmd[0].trim; 
String[] numbers = cmd[1].split("\\s+"); 

數組numbers將包含數字字符串標記,已修剪並準備通過Integer.parseInt()解釋。

+0

好吧我會試試這個 – TopRS 2012-08-03 23:23:41

0

從異常看來,無論是傳遞給int轉換例程都不是可分析的整數。

它實際上看起來像你的程序沒有正確解析輸入字符串,而在數字中留下序列,可能是因爲你使用了「\ t」。我會檢查與正則表達式,看看你是否真的可以找到這些標籤,並正確地拆分這個文件,然後再轉換爲整數。

你也可以成爲一名防禦性程序員,在將字符串實際轉換爲int之前,檢查字符串是否可以轉換爲int。

public static boolean isNumeric(String aStringValue) { 
    Pattern pattern = Pattern.compile("\\d+"); 
    Matcher matcher = pattern.matcher(aStringValue); 
    return matcher.matches(); 

}

而且,只是爲了調試試:

s = s.replaceAll("\\t","|"); 

,看看它的樣子。

0

正確的,因爲這樣的:

"1923  3208 3214 1  0  2  " 

不是數字。異常看起來非常清楚 - 您需要將該行標記爲數字部分,然後逐個分析。

相關問題