java 用Java读取CSV文件并将值存储在一个int数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14114358/
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
Reading CSV file in Java and storing the values in an int array
提问by James
I have a CSV file of strings in this format:
我有一个这种格式的字符串 CSV 文件:
14/10/2011 422 391.6592 394.52324 0.039215686
13/10/2011 408.43 391.7612 395.0686031 0.039215686
12/10/2011 402.19 391.834 395.3478736 0.039215686
All I want to do is read in the csv file and then store the 3rd and 4th coloumns data in integer arrays.
我想要做的就是读取 csv 文件,然后将第 3 和第 4 列数据存储在整数数组中。
This is the code I have written:
这是我写的代码:
BufferedReader CSVFile =
new BufferedReader(new FileReader("appleData.csv"));
String dataRow = CSVFile.readLine();
int count = 0;
while (dataRow != null){
String[] dataArray = dataRow.split(",");
EMA[count] = dataArray[2];
SMA[count] = dataArray[3];
dataRow = CSVFile.readLine(); // Read next line of data.
}
// Close the file once all data has been read.
CSVFile.close();
I want to end up with two arrays, EMA which contains all the values from the 3rd coloumn and SMA which contains the values from the 4th coloumn.
我想最终得到两个数组,EMA 包含第 3 列的所有值,SMA 包含第 4 列的值。
I am getting a null pointer exception. Can someone please tell me what mistake I am making?
我收到空指针异常。有人可以告诉我我犯了什么错误吗?
回答by duffymo
Your file appears to use whitespace/tab as a delimiter, but you're splitting at commas. That makes no sense to me.
您的文件似乎使用空格/制表符作为分隔符,但您以逗号分隔。这对我来说毫无意义。
You assume that the data row has a certain length without checking it. That makes no sense to me.
你假设数据行有一定的长度而不检查它。这对我来说毫无意义。
This code will show you how to do it better:
此代码将向您展示如何做得更好:
package cruft;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* CsvParser
* @author Michael
* @link http://stackoverflow.com/questions/14114358/reading-csv-file-in-java-and-storing-the-values-in-an-int-array/14114365#14114365
* @since 1/1/13 4:26 PM
*/
public class CsvParser {
public static void main(String[] args) {
try {
FileReader fr = new FileReader((args.length > 0) ? args[0] : "resources/test.csv");
Map<String, List<String>> values = parseCsv(fr, "\s+", true);
System.out.println(values);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, List<String>> parseCsv(Reader reader, String separator, boolean hasHeader) throws IOException {
Map<String, List<String>> values = new LinkedHashMap<String, List<String>>();
List<String> columnNames = new LinkedList<String>();
BufferedReader br = null;
br = new BufferedReader(reader);
String line;
int numLines = 0;
while ((line = br.readLine()) != null) {
if (StringUtils.isNotBlank(line)) {
if (!line.startsWith("#")) {
String[] tokens = line.split(separator);
if (tokens != null) {
for (int i = 0; i < tokens.length; ++i) {
if (numLines == 0) {
columnNames.add(hasHeader ? tokens[i] : ("row_"+i));
} else {
List<String> column = values.get(columnNames.get(i));
if (column == null) {
column = new LinkedList<String>();
}
column.add(tokens[i]);
values.put(columnNames.get(i), column);
}
}
}
++numLines;
}
}
}
return values;
}
}
Here's the input file I used to test it:
这是我用来测试它的输入文件:
# This shows that comments, headers and blank lines work fine, too.
date value1 value2 value3 value4
14/10/2011 422 391.6592 394.52324 0.039215686
13/10/2011 408.43 391.7612 395.0686031 0.039215686
12/10/2011 402.19 391.834 395.3478736 0.039215686
Here's the output I got:
这是我得到的输出:
{date=[14/10/2011, 13/10/2011, 12/10/2011], value1=[422, 408.43, 402.19], value2=[391.6592, 391.7612, 391.834], value3=[394.52324, 395.0686031, 395.3478736], value4=[0.039215686, 0.039215686, 0.039215686]}
Process finished with exit code 0
回答by Manidip Sengupta
[1] There should be a count++ inside the while loop
[1]while循环里面应该有count++
[2] You have not defined/initialized the arrays EMA and SMA - causing the exception.
[2] 您尚未定义/初始化数组 EMA 和 SMA - 导致异常。
[3] If you split() by comma and have a space separated file, the result will be an array of unity length, and indices 2 and 3 with generate NullPointerException - even if you initialize the arrays properly.
[3] 如果你用逗号 split() 并有一个空格分隔的文件,结果将是一个统一长度的数组,索引 2 和 3 会生成 NullPointerException - 即使你正确初始化了数组。
I suggest reading in the number by adding them to a List (like ArrayList or Vector) in the loop, since you do not know the size in advance. Once you get out of the loop, create 2 arrays of appropriate size and copyInto() the data in the arrays. Let the garbage collector deal with the Vectors.
我建议通过将它们添加到循环中的列表(如 ArrayList 或 Vector)来读取数字,因为您事先不知道大小。退出循环后,创建 2 个适当大小的数组并 copyInto() 数组中的数据。让垃圾收集器处理 Vectors。
回答by rlinden
The problem with your code is that int[] EMA is not an initialization. It just defines that EMA is an array of integers, without effectively creating it (you only have the reference).
您的代码的问题在于 int[] EMA 不是初始化。它只是定义了 EMA 是一个整数数组,而没有有效地创建它(你只有引用)。
My advice would be changing EMA and SMA to ArrayListsand instead of using attributions, you could add the current elements to the lists.
我的建议是将 EMA 和 SMA 更改为ArrayLists,而不是使用属性,您可以将当前元素添加到列表中。
In the end of the loop, you get the number of elements at each ArrayList using the size() method and can change them into arrays using toArray method, fulfilling whichever goal you might have.
在循环结束时,您可以使用 size() 方法获取每个 ArrayList 中的元素数量,并且可以使用 toArray 方法将它们更改为数组,从而实现您可能拥有的任何目标。
Of course, I am assuming that you forgot the commas at your example. Otherwise, you should change the delimiter to whitespace.
当然,我假设您忘记了示例中的逗号。否则,您应该将分隔符更改为空格。