1

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.

3
  • What is the problem you are encountering? Commented Oct 10, 2014 at 7:42
  • Where is the problem ? Commented Oct 10, 2014 at 7:44
  • sorry if i wasn't clear enough, i have edited my description. The problem is i am having trouble figuring out a way to store the average of each row (gpa) in another array Commented Oct 10, 2014 at 7:45

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

It is assumed that there are 8 grades per student, unfortunately i am unable to use ArrayLists
The second solution seems to work correctly, what if the grades were unknown beforehand?
Well, the ArrayList approach.
It's a rather "artificial" restriction to not be able to use List implementations. It's a very basic and useful datatype.
There would be no other solution without using an ArrayList?
|
1

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));
}

11 Comments

Actually the use of ArrayLists is prohibited for this solution
@AJJ To expand a little on the updated answer: Your problem was that you were trying to assign all the gpas after your while loop (which clears and resets the gpa each time. The second solution in this question moves the allocation to the gpaList into the while loop, so you store the data while it is still valid. - I hope this helps. @TheQuickBrownFox, feel free to add this (or similar) to the question.
@Baldrickk thank you for the explanation. For future purposes what would be the solution if the amount of grades was not known in advance?
@AJJ you'd have to do one or more of the following: 1) allocate an array larger than you think you would ever need before hand 2) stop and inform the user that it is incomplete (error) or 3) Use another data structure (one that can grow). Either ArrayList, or maybe another built-in data type that can grow like List.
@AJJ you can count the total line first, in another loop.
|
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.