我的项目有一个班级的16名学生的名字和分数。文本文件包含其名称和实验/测验分数。我需要将这些数据读入并行数组。
这就是文本文件的样子:
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
对于没有间隙线的每个学生,重复此操作。文件格式为
十六名学生中的每一名都会重复这种格式。
我正在努力学习如何将每行读入他们自己的数组。我需要为每个学生制作一个吗?因为最后我必须能够按年级对学生进行排序,但我并没有寻求帮助。就我如何将每个学生的数据及其成绩读入阵列而言。以及如何存储它们以便我能够区分哪些等级属于哪些学生。以下是我到目前为止所做的事情:
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 ;
}
}
答案 0 :(得分:0)
可能是这样的:
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();