在Java中将文本文件中的数据解析为多个数组

在Java中将文本文件中的数据解析为多个数组,java,parsing,fileinputstream,Java,Parsing,Fileinputstream,让我首先说我对Java相当陌生,所以如果我犯了明显的错误,请原谅我 我有一个文本文件,我必须从中读取数据,并将数据拆分为单独的数组 文本文件包含此格式的数据(但如有必要,可以稍加修改,使其具有标识符标记,如果这是唯一的方式) noOfStudents 学生姓名学生ID课程编号 courseName courseNumber学分时数等级 courseName courseNumber学分时数等级 courseName courseNumber学分时数等级 . . 学生姓名学生ID课程编号 cour

让我首先说我对Java相当陌生,所以如果我犯了明显的错误,请原谅我

我有一个文本文件,我必须从中读取数据,并将数据拆分为单独的数组

文本文件包含此格式的数据(但如有必要,可以稍加修改,使其具有标识符标记,如果这是唯一的方式)

noOfStudents
学生姓名学生ID课程编号
courseName courseNumber学分时数等级
courseName courseNumber学分时数等级
courseName courseNumber学分时数等级
.
.
学生姓名学生ID课程编号
courseName courseNumber学分时数等级
courseName courseNumber学分时数等级
courseName courseNumber学分时数等级
.
.

第一行表示将列出并需要移动到数组中的“学生”总数。 一个数组将包含学生信息,因此
学生姓名、学生ID、课程编号
到一个数组,并且
课程名称、课程编号、学分时数、年级
到第二个数组

我的问题源于如何解析这些数据。
我目前正在阅读第一行,转换为int,并使用它来确定我的学生数组的大小。 之后,我不知如何将数据移动到数组中,并让我的程序知道将哪些行移动到哪个数组中

需要注意的一点是,每个学生参加的课程数量是可变的,因此我不能简单地将一行读入一个数组,然后将三行读入下一个数组,以此类推

我是需要使用标识符,还是遗漏了一些明显的东西?我已经研究这个问题一周了,现在我只是感到沮丧

非常感谢您的帮助!多谢各位

编辑:这是我目前正在处理的代码部分

public static void main(String args[])
  {
  try{
  // Open the file
  FileInputStream fstream = new FileInputStream("a1.txt");
  // Get the object of DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));

  String strLine; // temporarily holds the characters from the current line being read
  String firstLine; // String to hold first line which is number of students total in     file.

  // Read firstLine, remove the , character, and convert the string to int value. 
  firstLine = br.readLine();
  firstLine = firstLine.replaceAll(", ", "");
  int regStudnt = Integer.parseInt(firstLine);
  // Just to test that number is being read correctly.
  System.out.println(regStudnt + " Number of students\n");

  // 2D array to hold student information
  String[][] students;
  // Array is initialized large enough to hold every student with 3 entries per student. 
  // Entries will be studentName, studentID, numberOfCourses
  students = new String[3][regStudnt];


  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
      // Split each line into separate array entries via .split at indicator character.
      // temporary Array for this is named strArr and is rewriten over after every line   read.
      String[] strArr;
      strArr = strLine.split(", ");
  }

  //Close the input stream
  in.close();
    }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
我希望这有助于有人引导我走向正确的方向

我想从这一点来看,我面临的主要问题是找到如何循环的方式,即学生信息被读取到学生数组,然后课程信息被读取到适当的课程数组位置,然后从一个新学生开始,直到所有学生都被读取。

有一个标识符(可能是空行)因为当一个新学生开始学习的时候,这会让学习变得容易,就像你所能做的那样

if("yourIdentifier".equals(yourReadLine))
    <your code for starting a new student>
if(“yourdidentifier.equals(yourReadLine))

以下是一些伪代码,可以让您走上正轨:

Student[] readFile() {
  int noOfStudents = ...;
  Student[] students = new Student[noOfStudents];

  for (int i = 0; i < noOfStudents; ++i) {
    students[i] = readStudent();
  }

  return students;
}

Student readStudent() {
  int numberOfCourses = ...;
  String name = ...;
  String id = ...;

  Course[] courses = new Course[numberOfCourses]

  for (int i = 0; i < numberOfCourses; ++i) {
    courses[i] = readCourse();
  }

  return new Student(id, name, courses);
}
Student[]readFile(){
int noOfStudents=。。。;
学生[]学生=新生[无学生];
对于(int i=0;i
试试这个代码段,我认为它完全符合您的要求。如果你有任何困惑,请告诉我!

class course {

        String name;
        int number;
        int credit;
        String grade;
    }

    class student {

        String name;
        String id;
        int numberCourses;
        course[] courses;
    }

    class ParseStore {

        student[] students;

        void initStudent(int len) {
            for (int i = 0; i < len; i++) {
                students[i] = new student();
            }
        }

        void initCourse(int index, int len) {
            for (int i = 0; i < len; i++) {
                students[index].courses[i] = new course();
            }
        }

        void parseFile() throws FileNotFoundException, IOException {
            FileInputStream fstream = new FileInputStream("test.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            int numberStudent = Integer.parseInt(br.readLine());
            students = new student[numberStudent];
            initStudent(numberStudent);

            for (int i = 0; i < numberStudent; i++) {

                String line = br.readLine();
                int numberCourse = Integer.parseInt(line.split(" ")[2]);
                students[i].name = line.split(" ")[0];
                students[i].id = line.split(" ")[1];
                students[i].numberCourses = numberCourse;
                students[i].courses = new course[numberCourse];
                initCourse(i, numberCourse);

                for (int j = 0; j < numberCourse; j++) {
                    line = br.readLine();
                    students[i].courses[j].name = line.split(" ")[0];
                    students[i].courses[j].number = Integer.parseInt(line.split(" ")[1]);
                    students[i].courses[j].credit = Integer.parseInt(line.split(" ")[2]);
                    students[i].courses[j].grade = line.split(" ")[3];
                }
            }                        
        }
    }
课程{
字符串名;
整数;
国际信贷;
串级;
}
班级学生{
字符串名;
字符串id;
国际数字课程;
课程[]课程;
}
类解析存储{
学生[]学生;
无效初始学生(整数长度){
对于(int i=0;i


在执行
ParseStore

之后,您可以通过打印
学生
数组的内容来测试它。这是家庭作业还是您希望/需要使用数组?如果是这样,请适当标记。@PhilippReichart:这显然是家庭作业,但这是一个没有实际意义的问题。对于原始海报,每个学生的数据之间是否有空行?您可以发布数据文件的示例吗?对于初学者,您可能希望读入每一行,使用
line.split(\\s)在空格中拆分它
然后根据您从该拆分中收到的元素数决定如何继续。文本文件中每个学生之间有一个空行您需要完整的源代码还是一些线索或伪代码?谢谢您的帮助!现在,我在这项家庭作业上正朝着正确的方向前进!你真厉害