Java.csv打印语句问题

Java.csv打印语句问题,java,arrays,csv,multidimensional-array,input,Java,Arrays,Csv,Multidimensional Array,Input,当前正在处理一个项目,该项目导入一个.csv文件(21行20列),将其捕获到一个数组中,然后在电子表格中打印一个特定的单元格。。。当前遇到一个问题,导致输出为20行一列,为“null”,但输出中的第二行似乎是文件中的最后一行、第二列单元格。空值是怎么回事?为什么它要提取最后一行数据?谢谢各位的意见 公共类cvsPull{ 公共字符串[][]myArray; 字符串csvFile=“Crime.csv”; 公共类csvPull(){ myArray=新字符串[20][20]; 试一试{ s=新扫描

当前正在处理一个项目,该项目导入一个.csv文件(21行20列),将其捕获到一个数组中,然后在电子表格中打印一个特定的单元格。。。当前遇到一个问题,导致输出为20行一列,为“null”,但输出中的第二行似乎是文件中的最后一行、第二列单元格。空值是怎么回事?为什么它要提取最后一行数据?谢谢各位的意见

公共类cvsPull{
公共字符串[][]myArray;
字符串csvFile=“Crime.csv”;
公共类csvPull(){
myArray=新字符串[20][20];
试一试{
s=新扫描仪(新BufferedReader(新文件读取器(csvFile));
而(s.hasNext()){
int theRow=1;
int theCol=0;
InputLine=s.nextLine();
字符串[]InArray=InputLine.split(“,”);
用于(字符串InArray1:InArray){
myArray[theRow][theCol]=inaray1;
theCol++;
如果(COL==20){
胆固醇=0;
theRow++;
}
//System.out.println(myArray[theRow][theCol]);
}
} 
for(字符串[]字符串:myArray){
System.out.println(字符串[1]);
}
}捕获(ioe异常ioe){
System.out.println(“不正确的文件名”+ioe.getMessage());
}
}

在每个循环开始时,将行计数器重置为1:

    while (s.hasNext()) {
        int theRow = 1;
        int theCol = 0;
这意味着文件的每一行都会写入内存中的同一位置。此外,行的第一个索引为0,这与列不同,因此您首先要将其设置为0:

    int theRow = 0;
    while (s.hasNext()) {
        int theCol = 0;

我建议使用lib读取CSV文件:


感谢您的反馈。while(s.hasNext())可以用什么来代替
,因此,与其检查每一行并打印,不如只打印输入的单行。您所说的“单行输入”是什么意思?之前您说过您的输入文件有21行,我的意思是只打印一行(行)从csv文件,而不是整个第一列,听起来像是要循环遍历给定行中的字符串(列):
for(String theString:myArray[row])System.out.println(theString);
感谢您的反馈。但我目前正在做的是将文件数据存储到与main()不同的类中的数组中。我在使用get方法拉入main()或导入文件时没有问题。我的问题更多的是修复代码拉入的特定数据。
import com.opencsv.CSVReader;

import java.io.FileReader;
import java.io.IOException;

public class CSVReaderExample {

    public static void main(String[] args) {

        String csvFile = "/Users/mkyong/csv/country3.csv";

        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(csvFile));
            String[] line;
            while ((line = reader.readNext()) != null) {
                System.out.println("Country [id= " + line[0] + ", code= " + line[1] + " , name=" + line[2] + "]");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}