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

标签 java parsing fileinputstream

首先让我说我是 Java 的新手,所以如果我犯了明显的错误请原谅我......

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

文本文件包含这种格式的数据(尽管如果有必要,可以稍微修改它以具有标识符标签,如果这是唯一的方式的话)

没有学生
studentNAme studentID numberOfCourses
courseName courseNumber 学分学分等级
courseName courseNumber 学分学分等级
courseName courseNumber 学分学分等级
.
.
studentNAme studentID numberOfCourses
courseName courseNumber 学分学分等级
courseName courseNumber 学分学分等级
courseName courseNumber 学分学分等级
.
.

第一行表示将列出并需要移动到数组的“学生”总数。 一个数组将包含学生信息,所以
studentName, studentID, numberOfCourses
到一个数组,并且
courseName, courseNumber, creditHours, grade
到第二个数组。

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

需要注意的是,每个学生选修的类(class)数量是可变的,所以我不能简单地将 1 行读入一个数组,然后将 3 行读入下一个数组,依此类推。

我是否需要使用标识符,或者我是否遗漏了一些明显的东西?我已经研究这个问题一个星期了,现在我感到很沮丧。

非常感谢任何帮助!谢谢

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

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());
  }
  }

我希望这有助于有人引导我朝着正确的方向前进。

我想我现在遇到的主要问题是找出如何循环,将学生信息读取到学生数组,然后将类(class)信息读取到适当的类(class)数组位置,然后重新开始与新学生一起阅读,直到所有学生都读完。

最佳答案

试一试这段代码,我认为它完全符合您的要求。如果您有任何困惑,请告诉我!

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];
                }
            }                        
        }
    }


您可以在 ParseStore 执行后打印 students 数组的内容来测试它

关于java - 将文本文件中的数据解析为Java中的多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7562093/

相关文章:

java - Eclipse 继续运行我的旧 Web 应用程序

java - 1 java内存刷新 volatile : A good programdesign?

java - xml解析+Java ME

android - 如何在android中使用tagsoup解析xml中的html内容

java - 使用 FileStream 在 Java 中复制文件

java - 压缩数据并将其上传到 S3,而不将全部内容保存在内存中

http - 将数据传递给嵌套模板未按预期工作

java - 使用 JSoup 删除 HTML 实体同时保留换行符

Java 在读取任何内容之前关闭 FileInputStream

.pdf 文件的 java 文件复制程序在输出中获取损坏的文件