2011-09-03 178 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"); 
    }