2011-09-03 173 views
7

我试图从另一个Activity中生成的文本文件导入文本。生成的文本文件由仅包含数字和Android生成的其他随机文本的StringArrayList组成。当我从文件中导入文本时,我使用BufferedReaderreadLine()将每个新号码变为IntegerArrayList。我将从文本文件中删除任何非数字值,并将其他活动中生成的数字拆分为“\ n”。由于Integer.parseInt导致崩溃

是我面临的问题是,当它加载Activity Android的崩溃。我已将事业范围缩小至Integer.parseInt()

我的代码如下:

ArrayList<Integer> lines = new ArrayList<Integer>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     File file = new File(getFilesDir(), "test_file.txt"); 

     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      while (br.readLine() != null) { 
       String text = (br.readLine()).replaceAll("[^0-9]+","").trim(); 
       Integer number = Integer.parseInt(text); 
       lines.add(number); 
      } 
     } catch (IOException e) { 

     } 

     TextView tv = (TextView) findViewById(R.id.helptext); 

     int max = 0, min = 100; 
     double total = 0; 
     for (int i = 0; i < lines.size(); i++) { 
      int number = lines.get(i); 
      max = Math.max(max, number); 
      min = Math.min(min, number); 
      total += number; 
     } 

     tv.setText("max = " + max + " min = " + min + " total = " 
       + total); 

回答

10

问题:

  • 当你做replaceAll("[^0-9]+","")您可以用空字符串造成Integer.parseInt抛出NumberFormatException结束。

  • 你跳过每隔一行(你while循环条件消耗第一行,第三行等等...)

    while (br.readLine() != null) // consumes one line 
    

尝试是这样的:

BufferedReader br = new BufferedReader(new FileReader(file)); 
String input; 
while ((input = br.readLine()) != null) { 
    String text = input.replaceAll("[^0-9]+",""); 
    if (!text.isEmpty()) 
     lines.add(Integer.parseInt(text)); 
} 
+0

IT WORKS !!!!!!我不得不将text.isEmpty()改为text.length()== 0,因为我使用的是较旧的Android API,但它工作正常! 谢谢你这么多DACWE! – BGM

+1

@Arjan - 对不起,我确实已经阅读了关于在评论中回复的帮助,但是却意外地忽略了“@”符号。另外,对于发球台我很抱歉,我真的没有想到会造成太多麻烦。我花了很长时间来解决这个问题,并且在达克解决这个问题时非常开心,我无法对他表示感谢。我向你保证它不会再发生。顺便说一句,我不得不说,stackoverflow是最好的,如果不是最好的,在互联网上的代码解决方案的网站之一。如果你是帮助建立的人之一,非常感谢你! – BGM

+1

@BGM,dacwe的tee不是问题,如果你阅读[this](http://meta.stackexchange.com/questions/104959/gift-reward-offered-after-accepting-an-answer)。最后一点,''堆栈溢出在它的名字中占用一个空格;-)''。所有这一切说:让我们清理;我正在删除我的评论 - 欢迎来到Stack Exchange! – Arjan

0

如果您将数字作为字符串,如“1234”它不会给出任何异常或错误。但是你会给任何字符或特殊字符,然后parse()函数会抛出异常。所以,请仔细检查,必须有一些字符传递,因此抛出异常,并获得坠毁

+0

感谢您的帮助! :) – BGM

+0

你的问题解决了吗? –

+0

是的,所有解决!谢谢! – BGM

0

确保text只有数字字符串中的,很可能不是。你也可以试试:

Integer number = Integer.valueOf(text); 

代替:

Integer number = Integer.parseInt(text); 

参见:

parseInt函数()返回基本整数类型(INT),其中的valueOf 回报的java.lang .Integer,其代表 整数的对象。有些情况下,你可能会想,而不是原始类型的整数 对象。

编辑:下面您的意见,我会在循环每次登录text后,没准什么时候它会引发错误的日志将显示text变量是空的。

+0

不幸的是,在尝试你的代码后,该应用程序仍然崩溃。我在其他活动中为文件生成我的数组列表的方式是: – BGM

+0

'code'String filename =“test_file.txt”; \t \t FileOutputStream fos; \t \t尝试{ \t \t \t fos = openFileOutput(filename,Context.MODE_PRIVATE); \t \t \t ObjectOutputStream out = new ObjectOutputStream(fos); \t \t \t out.writeObject(arrayList); \t \t \t out.close(); \t \t}赶上(FileNotFoundException异常E){ \t \t \t // TODO自动生成的catch程序块 \t \t \t e.printStackTrace(); \t \t}赶上(IOException的发送){ \t \t \t // TODO自动生成的catch程序块 \t \t \t e.printStackTrace(); 'code' ........'code'arrayList.add(Integer.toString(val [0])+“\ n”);'code' – BGM

+0

**我已经检查过'code'text'code'在textView下打印出来,使用replaceAll(“[^ 0-9] *”,“”)函数后字符串中只有数字。 – BGM

2

以上所有答案都是真实的,但如果他们不帮你,因为某些原因数据来你是不是一个Integer。例如 - 服务器错误地发送给你的用户名而不是userId(应该是一个整数)。

这可能发生,所以我们必须始终放在检查,以防止它。否则,我们的应用程序将崩溃,这不会是一个愉快的用户体验。因此,在将String转换为Integer时,请始终使用try-catch块来防止应用程序崩溃。我使用以下代码来防止由于整数解析导致应用程序崩溃 -

try { 
    Log.d(TAG, Integer.parseInt(string)); 
    } catch (NumberFormatException e) { 
     Log.w(TAG, "Key entered isn't Integer"); 
    }