多行文本读入后排序
【问题】
My project has names and scores for 16 students in a class. 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
-
Line 2: lab grades
-
Line 3: quiz grades
-
Line 4: project grades
-
Line 5: exam grades, and
-
Line 6: final exam grade.
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 ;
}
}
【回答】
数据每6行对应一条记录,要求对记录排序。其中final_exam_grade是单值,直接排序就行,lab grades是集合,要求平均后再排序。类似的结构化计算用JAVA编码复杂度很大。可以用SPL完成,再嵌入Java(参考Java 如何调用 SPL 脚本):
A |
|
1 |
=file("d:\\data.txt").import@is() |
2 |
=A1.(substr(~,":")) |
3 |
=A2.group((#-1)\6).new(~(1):name, ~(2).split@t(" "):lab_grades, ~(6):final_exam_grade) |
4 |
=A3.sort(final_exam_grade) |
5 |
=A3.sort(-lab_grades.avg()) |
A1:读取文本文件data.txt中的内容。
A2:提取:后边的数据。
A3:每6行数据分成一组,并创建以为name、lab_grades和final_exam_grade为列的序表,其中lab_grades列的成员为序列。
A4:结果按照final_exam_grade升序排列。
A5:结果按照lab_grades平均值降序排列。