I have some code that reads a row of characters from an array, assign the characters to an integer and then average all the integers of the row, it does this for every row in the array. As i am new to java i am having trouble trying to store the average of each row (gpa) in another array. Here is what I have so far :
Scanner in = new Scanner(new File("Grades.txt"));
Scanner in2 = new Scanner(new File("Grades.txt"));
int size = 0;
while(in2.hasNextLine()) {
size++;
}
int count = 0;
double [] gpalist = new double[size] ;
while(in.hasNextLine()) {
size++;
double gp = 0;
double gpa = 0;
String line = in.nextLine();
double [] arraygpa = new double [7];
if(line.length() == 0) {
continue;
}
line = line.substring(line.length()-15, line.length());
String[] letters = line.split(" ");
for (int i = 0; i < letters.length; i++) {
if (letters[i].equals("H")) {
gp += 7;
}
else if (letters[i].equals("D")) {
gp += 6;
}
else if (letters[i].equals("C")) {
gp += 5;
}
else if (letters[i].equals("P")) {
gp += 4;
}
else if (letters[i].equals("F")) {
gp += 0;
}
}
gpalist[count++] = gp / letters.length;
System.out.println(Arrays.toString(gpalist));
}
Here is the current output :
[5.75]
[6.75, 0.0]
[4.375, 0.0, 0.0]
[2.375, 0.0, 0.0, 0.0]
[4.125, 0.0, 0.0, 0.0, 0.0]
[4.5, 0.0, 0.0, 0.0, 0.0, 0.0]
[2.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
I am trying to store the averages (gpa) in another array but my attempt is far from correct. The use of ArrayLists for the solution is prohibited unfortunately. I would be grateful if anyone could point me in the right direction.