[英]How do I store int values from CSV file in separate two-dimensional array elements
If I have a CSV file with 4 numbers on one line separated by a comma, how do I ignore the first two values using openCSV?如果我有一个 CSV 文件,一行中有 4 个数字,以逗号分隔,如何使用 openCSV 忽略前两个值?
Now, consider the following two-dimensional array:现在,考虑以下二维数组:
int[][] data = new int [10][10];
and the following line from a CSV file:以及来自 CSV 文件的以下行:
54, 68, 5, 1
assuming the former is possible (ignoring the first two values on a line in a CSV file), how do I then parse the value '5' into data[0][0] and parse the value '1' into data[0][1]?假设前者是可能的(忽略 CSV 文件中一行的前两个值),那么我如何将值 '5' 解析为 data[0][0] 并将值 '1' 解析为 data[0] [1]?
ie: IE:
data[0][0] = 5;
data[0][1] = 1;
Of course, if you cannot ignore the first two values, how do I parse each value (54, 68, 5, and 1) all in separate array elements?当然,如果你不能忽略前两个值,我如何解析每个值(54、68、5 和 1)都在单独的数组元素中?
ie: IE:
data[0][0] = 54;
data[0][1] = 68;
data[0][2] = 5;
data[0][3] = 1;
I can't find anything in the openCSV documentation that would explain how to do this, nor can I wrap my head around even doing it if there weren't a CSV file to read beforehand.我在 openCSV 文档中找不到任何可以解释如何执行此操作的内容,如果没有要事先阅读的 CSV 文件,我什至无法做到这一点。
Just googling "opencsv" got me to its project page .只是谷歌搜索“opencsv”让我进入了它的项目页面。 That page has the following example:
该页面有以下示例:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
What prevents you from doing the same and just accessing the fields you want by index?是什么阻止你做同样的事情,只是通过索引访问你想要的字段?
data[0][0] = nextLine[2];
data[0][1] = nextLine[3];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.