2012-11-19 29 views
4

我是一個Java noob以及這個網站非常新,所以請忍受我在這裏。如果我在發佈這篇文章時做錯了事,請讓我知道,我事先爲我碰巧做的事或任何錯誤的語法道歉。我找不到是什麼導致例外

我需要在java中編寫一個自定義CSV解析器,它不會在引號內分隔逗號。我無法使用與CSV類相關的任何內容。 1,2,3,4 - > 1 2 3 4 一, 「B,C」,d - > AB,CD

無論我怎麼努力,我總是得到一個異常

import java.io.*; 
import java.util.*; 

public class csv 
{ 
    public static void main(String[] args) 
    { 
     File file = new File("csv.test"); 
     BufferedReader buf = null; 

     try 
     { 
      buf = new BufferedReader(new FileReader(file)); 

      String str; 

      while ((str = buf.readLine()) != null) 
      { 
       String[] values = null; // array for saving each split substr 
       int c1 = 0; // counter for current position in string 
       int c2 = 0; // counter for next space in array to save substr 
       int c3 = 0; // location of last comma or quotation for substring creation 
       int i = 0; // counter used later for printing 

       while (c1 <= str.length()) 
       { 
        char x = str.charAt(c1); 
        String s = Character.toString(x); 

        if (c1 == str.length()) 
        { 
         values[c2] = str.substring(c3, (c1 - 1)); 
        } 
        else if (s == ",") 
        { 
         values[c2] = str.substring(c3, (c1 - 1)); 
         c1++; 
         c2++; 
         c3++; 
        } 
        else if (s == "\"") 
        { 
         c1++; 
         c3 = c1; 
         while (s != "\"") 
          c1++; 
         values[c2] = str.substring(c3, (c1 - 1)); 
         c1 = c1 + 2; 
         c2++; 
         c3 = c1; 
        } 
        else 
        { 
         c1++; 
        } 
        while (i < values.length) 
         System.out.println("\"" + values[i] + "\""); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      System.out.println("Error locating test file"); 
     } 
    } 
} 

我曾嘗試過切斷所有邏輯並測試它是否與文件io相關。它讀取的很好,因此它與邏輯有關。我查看了一下,找不到任何問題。我甚至有一位朋友看過它,並說它看起來很好。拋出異常在哪裏?

+3

可以請您包括完整的堆棧跟蹤?那將是一個好開始...... – mre

+0

你可以分享相關的堆棧跟蹤嗎? –

+0

將'e.printStackTrace();'添加到'catch'塊並告訴我們它說了什麼...... –

回答

3

您並未在此行中初始化您的valuesString[] values = null;因此,當您使用它時,即在列表values[c2] = str.substring(c3, (c1 - 1));處將會失敗。

要解決該問題,請使用適當的長度(例如,)來初始化values陣列。 String[] values = new String[SIZE];。可能您需要使用str.length()作爲陣列的SIZE

同樣在您的比較else if (s == ",")中,您使用==來比較字符串,這是不正確的。相反,您可以使用x本身作爲else if (x == ',')

編輯:以下情況會使c1無限增加,因爲您不會在任何地方更改str(x after correction as advised)的值。

舊條件:

   while (s != "\"") 
        c1++; 

變化通知(還是錯了,因爲X不是while循環內改變)後:

   while (x != '"') 
        c1++; 

請更正循環邏輯。

+0

當我解決這個問題時,我收到了第46行的OutOfBounds異常。這是爲什麼? – user1834642

+0

@ user1834642:我相信你的第46行是這樣的:'values [c2] = str.substring(c3,(c1-1));'如果是這樣,你的計數器即'c2,c3&c1'超出了字符串長度。請檢查邏輯。 –

+0

@ user1834642:你在行'while while(s!=「\」「)期待什麼 c1 ++;'。這會導致你的c1 ++無限增加,因爲你沒有改變'x'的值 –

1

與具體問題無關,但不是s == ",",您應該執行",".equals(s),對於其他字符串相等性檢查類似。

0

這不是你的異常的原因,但它顯然是不正確不過:

while (s != "\"") 
     c1++; 

這是不正確的原因有兩個:

  • 由於c1++不會改變循環條件,這將永遠循環。
  • 如果您應該使用equals(...),則正在使用==/!=來比較字符串。你似乎也在其他地方犯了這個錯誤。

要找出是什麼原因造成的例外,你應該做的第一件事是打印出堆棧跟蹤。將catch塊添加e.printStackTrace();

或者更好的是,改變它只是趕上IOException。捕捉Exception是一個壞主意......除非你打算記錄/輸出完整的堆棧跟蹤。

+0

什麼是相當於!= if ==是s.equals()。 – user1834642

+0

@ user1834642 - '!s.equals()' –

相關問題