java 将 CSV 文件导入二维字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16784014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing CSV file into 2D String array
提问by user2427023
I have to read a text file into a 2d Array.
我必须将文本文件读入二维数组。
The only problem I am having, is the width of the array varies, with a maximum size of 9 columns. I don't know how many rows there will be.
我遇到的唯一问题是数组的宽度各不相同,最大大小为 9 列。我不知道会有多少行。
Some lines will have 6 columns for example, and some will have 9.
例如,有些行将有 6 列,有些则有 9 列。
here is a small section of my CSV file:
这是我的 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,8,20000
1912,Easts,Glebe,Easts,Wests,N
1913,Easts,Newtown,Easts,Wests,N
and here is my code so far
到目前为止,这是我的代码
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]);
}
}
}
The error I get is:
我得到的错误是:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at ass2.readData(ass2.java:23)
at ass2.main(ass2.java:7)
Can someone please help :'(
有人可以帮忙吗:'(
Thank you!
谢谢!
采纳答案by Ted Hopp
The problem is with your inner loop. You are trying to access 9 elements of value
regardless of how many values there are on the line. First, you should move the assignment to value
to be before the inner loop. Then, you need to limit the loop iterations to the minimum of 9 and the length of value
:
问题出在你的内循环上。value
无论行上有多少个值,您都试图访问 9 个元素。首先,您应该将分配移动到value
内部循环之前。然后,您需要将循环迭代次数限制为最小值 9 和长度value
:
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"
}
Note that the trailing elements of data
will be null
.
请注意, 的尾随元素data
将是null
。
回答by Paul Vargas
回答by gkalpak
You get the error, because you try to access a 7th token (index 6) on a line that contains only 6. Replace that:
您会收到错误消息,因为您尝试访问仅包含 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"
}
with this:
有了这个:
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[]
}
You could, in fact, replace the whole while-loop
body with this one-liner:
事实上,你可以while-loop
用这个单衬替换整个身体:
dataArr.add(Arrays.copyOf(line.split(",", 9), 9));
See, also, this short demo.
另请参阅此简短演示。
回答by Subhrajyoti Majumder
Instead of array, you can use ArrayList
of List
. As List is dynamically growable so you do't need to think of it size either.
您可以使用ArrayList
of代替数组List
。由于 List 是动态可增长的,因此您也不需要考虑它的大小。
List<List<String>> dataArr = new ArrayList<List<String>>();
and
和
while ((line = dataBR.readLine()) != null){
for (int i = 0; i < 9; i++)
dataArr.add(Arrays.asList(line.split(",", 9)));
}