我有.csv数据文件要解析,并为此使用java语言。因此,我理解了如何使用数据并将其放入变量中,例如,使用二维数组:
//working with 2-dimensial Arrays
int[][] values = { {1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25} };
//output of the 1st column, 1st line array value
System.out.println(values[0][0]);
ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles", "dough"));
String firstName = list.get(0);
String secondName = list.get(1);
System.out.println(firstName);
但我需要先读取.csv文件,然后创建多维数组。在此之后,我需要拾取一些数据并将其放入变量中。我读csv没有任何具体的库,只有这样的标准:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
String fileName= "datatry.csv";
File file= new File(fileName);
List<List<String>> lines = new ArrayList<>();
Scanner inputStream;
//this scanner of csv doesn't work without try-catch pair
try{
//read the csv file
inputStream = new Scanner(file);
while(inputStream.hasNext()){
String line= inputStream.next();
String[] values = line.split(";");
// this adds the currently parsed line to the 2-dimensional string array
lines.add(Arrays.asList(values));
}
//System.out.println(value);
inputStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
// String newst = lines.get(1);
//System.out.println(newst);
// the following code lets you iterate through the 2-dimensional array
int lineNo = 1;
for(List<String> line: lines) {
int columnNo = 1;
for (String value: line) {
System.out.println("Line " + lineNo + " Column " + columnNo + ": " + value);
columnNo++;
}
lineNo++;
}
}}
我想我需要使用Object。
如何将.csv文件的第三列和第五行的值放入整型变量中?
1条答案
按热度按时间wwtsj6pe1#
伪代码:
1.设置targetRoNumber和targetColNumber
1.创建2个for循环来跟踪索引。第一个循环用于行,第二个循环用于列。
1.创建if条件以匹配目标行号和列号
或者如果你不想循环,直接进入索引