【发布时间】:2013-11-29 10:08:46
【问题描述】:
我正在使用一个 csv 将每一行与另一个 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");
}
}
【问题讨论】:
-
您没有告诉我们异常在哪里,也没有告诉我们文件的内容是什么——尤其是虚线。您执行了哪些诊断来找出问题所在?
-
使用这样的代码,您至少可以给我们一个实际的错误消息。
-
对不起,它应该是一个永远不会再次使用的快速程序。错误在线 if (match == false) { bw.write(customer[0] ..... 令人讨厌的是它运行了一些数据然后失败但所有数据的格式都相同。跨度>
-
你能把文件的内容贴出来吗?
-
错误代码是:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 3 at excel.parse.ExcelParse.main(ExcelParse.java:61) Java 结果:1
标签: java csv indexoutofboundsexception