Java 逐行读取文件,然后将字符串转换为二维整数数组

Java 逐行读取文件,然后将字符串转换为二维整数数组,java,arrays,string,multidimensional-array,Java,Arrays,String,Multidimensional Array,我试图一次只读取一行文本文件中的信息。考虑下面的示例文件: 学生1,45,32,45 学生2,34,22,23 然后我想将数据存储在字符串数组中,然后将字符串数组转换为二维整数数组,因为我需要能够操纵它们的分数。不幸的是,学生人数不详 我决定创建一个for循环,逐行读取数据,将其存储在字符串数组中,然后最终将其转换为二维整数数组 我的代码没有按预期工作 int [][]score = null; String [][]studentDataParsed = null; for (int i

我试图一次只读取一行文本文件中的信息。考虑下面的示例文件:

学生1,45,32,45
学生2,34,22,23
然后我想将数据存储在字符串数组中,然后将字符串数组转换为二维整数数组,因为我需要能够操纵它们的分数。不幸的是,学生人数不详

我决定创建一个for循环,逐行读取数据,将其存储在字符串数组中,然后最终将其转换为二维整数数组

我的代码没有按预期工作

int [][]score = null;
String [][]studentDataParsed = null;

for (int i = 0; i < 100; ++i) {
    for (int j = 0; j < 100; ++j) {
        String oneStudentData = input.nextLine(); //read one line from input file
        oneStudentData.split("[,]"); //parse the data
        studentDataParsed[i][j] = oneStudentData; 
        //convert the data into the double array                
    }
}

for (int i = 0; i < studentDataParsed.length; ++i) {
    for (int j = 0; i < studentDataParsed.length; ++j) {
        score[i][j]=(int) (Float.valueOf(studentDataParsed[i [j])).floatValue();
    }
} 
int[][]分数=null;
字符串[][]studentDataParsed=null;
对于(int i=0;i<100;++i){
对于(int j=0;j<100;++j){
字符串oneStudentData=input.nextLine();//从输入文件中读取一行
oneStudentData.split(“[,]”;//解析数据
studentDataParsed[i][j]=一个StudentData;
//将数据转换为双数组
}
}
for(int i=0;i
我不确定您在使用循环做什么。您应该已经在使用循环读取行,所以您只需要跟踪行并将拆分字符串分配给数组:

int curline = 0;
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    String line;
    while ((line = br.readLine()) != null) {
       studentDataParsed[curline] = line.split(",");
       curline++;
    }
}

您还可以考虑创建一个学生类,将每个行读入该类的构造函数,并使用一个列表来存储结果。