简体   繁体   English

将数据从txt文件读取到Java中的并行数组中?

[英]Read data from txt file into parallel arrays in Java?

My project has names and scores for 16 students in a class. 我的项目有一个班级的16名学生的名字和分数。 A text file has their names and their lab/quiz scores. 文本文件包含其名称和实验室/测验分数。 I need to read this data into parallel arrays. 我需要将此数据读取到并行数组中。

This is what the text files looks like: 文本文件如下所示:

Line 1: Puckett, Karen
Line 2: 10 10 9.5 10 10 8 9.5 10 10 10 9 10 10 10 0
Line 3: 4 3 5 3 5 2 3 2 1.5 1 5 3.5
Line 4: 17.5 24 22 23.5 22 23
Line 5: 90 91
Line 6: 96

This is repeated for each student with no gap lines. 对于没有空位线的每个学生重复此步骤。 The format in the file is 文件中的格式为

  • Line 1: name 第1行:名称
  • Line 2: lab grades 第2行:实验室等级
  • Line 3: quiz grades 第3行:测验成绩
  • Line 4: project grades 第4行:项目等级
  • Line 5: exam grades, and 第5行:考试成绩,以及
  • Line 6: final exam grade. 第6行:期末考试成绩。

This format is repeated for each of the sixteen students. 对十六名学生中的每一个重复此格式。

I'm struggling with how to read them into each line into their own arrays. 我正在努力如何将它们读入每一行到它们自己的数组中。 Would I need to make one for each student? 我需要为每个学生做一个吗? Because in the end I must be able to sort the students by grade, but I'm not asking for help with that. 因为最后我必须能够按年级对学生进行排序,但是我并没有寻求帮助。 Just on how I would read the data for each student and their grades into arrays. 关于如何将每个学生的数据及其分数读入数组。 And how to store them so that I would be able to distinguish which grades belong to which students. 以及如何存储它们,以便我能够区分哪个年级属于哪个学生。 Below is what I have done so far: 以下是我到目前为止所做的事情:

public class JavaApplication39 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        //declare file object, and connect to scanner  object
        File myFile = new File(System.getProperty("user.dir") + "/src/scores.txt");
        if (!myFile.exists()) {
            System.out.println("Unable to open the file");
            System.exit(0);
        }
        Scanner inputFile = new Scanner(myFile);

        //create method to return percent earned 
        //create name array
        String[] nameList = getNameList(inputFile);
        System.out.println(Arrays.toString(nameList));

        //create array of grades and then get average and return array of averages
        double[] labGrade = getLabGrade(inputFile);
        System.out.println(Arrays.toString(labGrade));
    }

    public static String[] getNameList(Scanner object) {
        String[] nameList = new String[16];
        while (object.hasNext()) {
            for (int i = 0; i < 16; i++) {
                nameList[i] = object.nextLine();
                object.nextLine();
                object.nextLine();
                object.nextLine();
                object.nextLine();
                object.nextLine();
            }
        }
        return nameList;
    }

    public static double[] getLabGrade(Scanner object) {
        double[] labGrade = new double[15];
        double[] labGradeSum = new double[15];
        int count = 0;
        object.nextLine();
        for (int i = 0; i < 15; i++) {
            labGrade[i] = object.nextDouble();
        }
        object.nextLine();
        object.nextLine();
        object.nextLine();
        object.nextLine();
        object.nextLine();
        object.nextLine(); 
    return labGrade ;
    }
}

maybe something like this: 也许是这样的:

 public class JavaApplication39 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        //declare file object, and connect to scanner  object
        File myFile = new File(System.getProperty("user.dir") + "/src/scores.txt");
        if (!myFile.exists()) {
            System.out.println("Unable to open the file");
            System.exit(0);
        }
        Scanner inputFile = new Scanner(myFile);  

           ArrayList<String> names = new ArrayList<>();
           ArrayList<String> labs = new ArrayList<>();
           ArrayList<String> quizes = new ArrayList<>();
           ArrayList<String> projects = new ArrayList<>();
           ArrayList<String> exams = new ArrayList<>();
           ArrayList<String> fExams = new ArrayList<>();


            int i = 1;
            while(inputFile.hasNext()){
                String line = inputFile.nextLine();   
                switch(i){
                case 1:  names.add(line);i++;
                break;
                case 2: labs.add(line);i++;
                break;
                case 3: quizes.add(line);i++;
                break;
                case 4: projects.add(line);i++;
                break;
                case 5: exams.add(line);i++;
                break;
                case 6: fExams.add(line);i++;i++;
                break;              
                }

                if(i >= 7){i=1;}
            }

            System.out.println(names.toString());
            System.out.println(labs.toString());
            System.out.println(quizes.toString());
            System.out.println(projects.toString());
            System.out.println(exams.toString());
            System.out.println(fExams.toString());

            inputFile.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM