ArrayIndexOutOfBoundsException

时间:2013-11-29 10:08:46

标签: java csv indexoutofboundsexception

我正在使用一个csv将每一行与另一个csv的每一行进行比较以找到匹配项。 然后我需要从第二个csv中添加一些来自第一个的元素并将其写入新文件。 它适用于csv的第一行,然后获取ArrayIndexOutOfBoundsException。 我理解数组如何工作,我已经检查了我的csv,据我所知,我不会超出界限。

第一个csv有8个字段,包含所有客户信息。第二个有15个字段,并持有客户的销售信息。如果有销售记录,前两个字段[0][1]在两个csv中都是相同的。

如果有人能快速浏览一下,我可能会错过一些愚蠢的事情。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at excel.parse.ExcelParse.main(ExcelParse.java:61)

package excel.parse;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;


public class ExcelParse {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String csvFile2 = "\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\Customer_Sales_Trends_Summary_by_Sales_Order_114641390.csv";
    String csvFile1 = "\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\All_Customers_Listing_for_Rep_114337469.csv";
    BufferedReader br = null;
    BufferedReader br2 = null;
    BufferedWriter bw = null;
    String line = "";
    String line2 = "";
    String csvSplitBy = ",";
    Boolean match = false;

    try {

        br = new BufferedReader(new FileReader(csvFile1));
        bw = new BufferedWriter(new FileWriter("\\\\SBS2011\\RedirectedFolders\\Josh.Hickinbotham\\My Documents\\newcsv.txt"));
        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] customer = line.split(csvSplitBy);
            System.out.println(customer[1]);
            br2 = new BufferedReader(new FileReader(csvFile2));
            while ((line2 = br2.readLine()) != null) {
                String[] file2 = line2.split(csvSplitBy);

                if (customer[1].equals(file2[1])) {
                    match = true;                        
                    bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
                        + customer[5] + "," + customer[6] + "," + customer[7] + ","+ file2[2] +","+ file2[3] + "," + file2[4] + "," + file2[5] + ","
                        + file2[6] + "," + file2[7] + "," + file2[8] + "," + file2[9] + "," + file2[10] + "," + file2[11] + "," + file2[12] + ","
                        + file2[13] + "," + file2[14]+"\r\n");
                    System.out.println(":::MATCH " +customer[1]+" : "+file2[1]+" :::");
                } else {
                    match = false;
                }
            }
            if (match == false) {
                bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
                    + customer[5] + "," + customer[6] + "," + customer[7] + "," + "," + "," + "," + "," + "," + "," + "," + "," + "," + ","
                    + "," + ","+"\r\n");
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (br2 != null) {
            try {
                br2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bw != null) {
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }          
    }
    System.out.println("Done");
}
}

3 个答案:

答案 0 :(得分:1)

尝试显示customer进入if match == false之前的内容。 可能有助于了解您正在处理什么。 :)

答案 1 :(得分:0)

如何简单检查“拆分”结果的界限?如果你在界限不正确的位置转储你的行,你会看到错误的输入数据......

答案 2 :(得分:0)

我认为问题出在包含csv的文件中。也许调试它并在获得异常的行停止以检查customer包含哪些数据。

但我想注意的是: 我不确定你的程序正在做它应该做的事情。当找到匹配时,它会将其打印出来,但随后继续进入内部while循环,因此最后match将再次为假,并打印出不匹配。 所以我想你想要break

if (customer[1].equals(file2[1])) {
     match = true;                        
     bw.write(customer[0] + "," + customer[1] + "," + customer[2] + "," + customer[3] + "," + customer[4] + ","
                    + customer[5] + "," + customer[6] + "," + customer[7] + ","+ file2[2] +","+ file2[3] + "," + file2[4] + "," + file2[5] + ","
                    + file2[6] + "," + file2[7] + "," + file2[8] + "," + file2[9] + "," + file2[10] + "," + file2[11] + "," + file2[12] + ","
                    + file2[13] + "," + file2[14]+"\r\n");
    break;
}