Reputation: 897
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.
Upvotes: 1
Views: 228
Reputation: 5102
Use ArrayList
if you are not sure how many lines in the text file:
ArrayList<Double> gpaList = new ArrayList<Double>();
while(in.hasNextLine()) {
int gp = 0;
double gpa = 0;
String line = in.nextLine();
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.Add(gp / letters.length);
}
If you insist to use Array, then you can do, Assuming that you the number of line in your txt file is not more than 8
int count = 0;
Double[] gpaList = new Double[8];
while(in.hasNextLine()) {
....
....
for (int i = 0; i < letters.length; i++) {
....
....
}
gpaList[count++] = gp / letters.length;
}
But if you don't know how many Grades are there, the nearest solution would be count the line first.
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()) {
....
....
for (int i = 0; i < letters.length; i++) {
....
....
}
gpaList[count++] = gp / letters.length;
System.out.println(Arrays.toString(gpalist));
}
Upvotes: 1
Reputation: 7157
Since you probably do not know the number of grades in advance, this is a good use case for the ArrayList.
List<Double> gpas = new ArrayList<Double>();
while(in.hasNextLine()) {
int gp = 0;
double gpa = 0;
...
gpa = gp / letters.length;
gpas.add(gpa);
...
} // End while
The ArrayList class has a couple of toArray methods, if you require a "simple" array later.
Update:
In the case where there are 8 grades per student, you can pre-initialize the array with that size, and keep an indexing variable outside the while-loop:
double[] gpas = new double[8];
int index = 0;
while(in.hasNextLine()) {
int gp = 0;
double gpa = 0;
...
gpas[index] = gp / letters.length;
index++;
...
} // End while
Upvotes: 1
Reputation: 799
So the problem is with storing all the line values? If so you just create list variable and store values inside. Like this:
List<Double> avrageGrades = new ArrayList<Double>();
and then when you have line avrage you just do this:
avrageGrades.Add(gpa);
If this is the function you can then just return avrageGrades when while ends.
But if you want an array, then you have to know nummber of lines. You first have to cound nummber of lines. Create an array with the size of number of the lines. And then have a counter inside of while so that you will know which line you have and then just do:
avrageGrades[line] = gpa;
Upvotes: 0