我正在尝试从.csv文件中获取某些数据并将其打印到输出。 String
类型没有带来任何问题,但处理int
制作让我感到奇怪。我试图从文件中创建一个数组并从中提取所需的元素,但代码根本不会编译。
package readcvs;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadCVS {
public static void main(String[] args) {
ReadCVS obj = new ReadCVS();
obj.run();
}
public void run() {
String csvFile = "/Users/hobbit/Desktop/E0.csv";
BufferedReader br = null;
String line = "";
String csvSplitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// comma is used as a separator
String[] tie = line.split(csvSplitBy);
System.out.println(tie[2] + " vs " + tie[3]);
String[] ref = line.split(csvSplitBy);
System.out.println ("Referee: " + ref[10]);
int digit = Integer.valueOf(line);
//convert String to integer
int[] shotsarr = new int[digit];
System.out.println("Home team shots: " + shotsarr[11]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}