2012-08-03 119 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  " 

不是数字。异常看起来非常清楚 - 您需要将该行标记为数字部分,然后逐个分析。

相关问题