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
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.