Java 将CSV文件导入二维字符串数组
我必须将文本文件读入2d数组 我遇到的唯一问题是数组的宽度不同,最大大小为9列。 我不知道会有多少排 例如,有些行有6列,有些行有9列 以下是我的CSV文件的一小部分:Java 将CSV文件导入二维字符串数组,java,arrays,csv,Java,Arrays,Csv,我必须将文本文件读入2d数组 我遇到的唯一问题是数组的宽度不同,最大大小为9列。 我不知道会有多少排 例如,有些行有6列,有些行有9列 以下是我的CSV文件的一小部分: 1908,Souths,Easts,Souths,Cumberland,Y,14,12,4000 1909,Souths,Balmain,Souths,Wests,N 1910,Newtown,Souths,Newtown,Wests,Y,4,4,14000 1911,Easts,Glebe,Glebe,Balmain,Y,11
1908,Souths,Easts,Souths,Cumberland,Y,14,12,4000
1909,Souths,Balmain,Souths,Wests,N
1910,Newtown,Souths,Newtown,Wests,Y,4,4,14000
1911,Easts,Glebe,Glebe,Balmain,Y,11,8,20000
1912,Easts,Glebe,Easts,Wests,N
1913,Easts,Newtown,Easts,Wests,N
这是到目前为止我的代码
import java.io.*;
import java.util.*;
public class ass2 {
public static void main(String[] args) throws IOException {
readData();
}
public static void readData() throws IOException{
BufferedReader dataBR = new BufferedReader(new FileReader(new File("nrldata.txt")));
String line = "";
ArrayList<String[]> dataArr = new ArrayList<String[]>(); //An ArrayList is used because I don't know how many records are in the file.
while ((line = dataBR.readLine()) != null) { // Read a single line from the file until there are no more lines to read
String[] club = new String[9]; // Each club has 3 fields, so we need room for the 3 tokens.
for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
String[] value = line.split(",", 9);
club[i] = value[i]; // Place the token into the 'i'th "column"
}
dataArr.add(club); // Add the "club" info to the list of clubs.
}
for (int i = 0; i < dataArr.size(); i++) {
for (int x = 0; x < dataArr.get(i).length; x++) {
System.out.printf("dataArr[%d][%d]: ", i, x);
System.out.println(dataArr.get(i)[x]);
}
}
}
有人能帮忙吗(
谢谢!您可以使用
List
的ArrayList
代替数组。因为列表是动态增长的,所以您也不需要考虑它的大小
List<List<String>> dataArr = new ArrayList<List<String>>();
List dataArr=new ArrayList();
及
while((line=dataBR.readLine())!=null){
对于(int i=0;i<9;i++)
dataArr.add(Arrays.asList(line.split(“,”,9));
}
问题在于您的内部循环。无论行上有多少个值,您都试图访问值的9个元素。首先,您应该将赋值移动到值
以位于内部循环之前。然后,您需要将循环迭代次数限制为最小9次,并且值的长度
:
String[] value = line.split(",", 9);
int n = Math.min(value.length, data.length);
for (int i = 0; i < n; i++) { // For each token in the line that we've read:
data[i] = value[i]; // Place the token into the 'i'th "column"
}
String[]value=line.split(“,”,9);
int n=Math.min(value.length,data.length);
对于(int i=0;i
请注意,数据
的尾随元素将为空
,因为您试图在仅包含6的行上访问第7个标记(索引6)。请替换该标记:
for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
String[] value = line.split(",", 9);
data[i] = value[i]; // Place the token into the 'i'th "column"
}
另请参见您可用于读取CSV文件的
// Read all
CSVReader csvReader = new CSVReader(new FileReader(new File("nrldata.txt")));
List<String[]> list = csvReader.readAll();
// Convert to 2D array
String[][] dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);
//全部读取
CSVReader CSVReader=新的CSVReader(新文件读取器(新文件(“nrldata.txt”));
List List=csvReader.readAll();
//转换为二维阵列
字符串[][]dataArr=新字符串[list.size()][];
dataArr=list.toArray(dataArr);
哪一行是代码的第49行?另外,你应该将这一行移动到循环之前。
字符串[]value=line.split(“,”,9);这不是ArrayIndexOutofBound的解决方案,但如果他使用了错误的索引,他最终可能会得到相同的结果。我提出了一个更好的解决方案,我想你搞错了:)事实上,您不需要“最小”部分,因为您将“9”作为第二个参数传递给split()
。非常感谢!:你是个救命的家伙@专家系统-这是真的。这是一种安全带和吊带的东西,以防代码演变和数据
以某种方式可能短于9(可能是无意中)。在这里使用符号常量比将文字9
分散在代码中更好。这使事情变得非常简单,我更喜欢这个,而不是公认的答案。我们应该导入什么?这应该被标记为答案!不错的一个@gkalpak
for (int i = 0; i < 9; i++) { // For each token in the line that we've read:
String[] value = line.split(",", 9);
data[i] = value[i]; // Place the token into the 'i'th "column"
}
String[] value = line.spkit(",", 9); // Split the line into max. 9 tokens
for (int i = 0; i < value.length; i++) {
data[i] = value[i]; // Add each token to data[]
}
dataArr.add(Arrays.copyOf(line.split(",", 9), 9));
// Read all
CSVReader csvReader = new CSVReader(new FileReader(new File("nrldata.txt")));
List<String[]> list = csvReader.readAll();
// Convert to 2D array
String[][] dataArr = new String[list.size()][];
dataArr = list.toArray(dataArr);