Вопрос

I'm having some difficulties trying to figure out how to implement a second array to calculate the averages of my marks program. I'm using 1 2D array to store the scores each student received on each assignment (5 students, 4 assignments). I have managed to get the Students average array working but when I go to calculate each individual assignment mark (combined total of the 5 students /5 ). I keep getting an Indexoutofbounds exception, Im very new to 2d arays and am still trying to figure out how to read them properly. Heres my code:

import java.util.Scanner;
public class Marks
{
private String[] names;
private int[][] assignments;
private double[] stuAvgArray;
private double[] assignAvgArray;
    public Marks()
    {
        names = new String[5];
        assignments = new int[5][4];
        stuAvgArray = new double[5];
        assignAvgArray = new double[4];
    }

public void getInput()
{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter 5 student names: ");
    for(int i = 0; i < names.length; i++)
        names[i] = input.nextLine();

    for(int row = 0; row < assignments.length; row++)
    {
        System.out.println("Enter the 4 marks for: " + names[row]);
        for(int col = 0; col < assignments[row].length; col++)
        {
            assignments[row][col] = input.nextInt();
            stuAvgArray[row] += assignments[row][col];

        }
        stuAvgArray[row] = (stuAvgArray[row]/4);
    }
    for (int col = 0; col < assignments.length; col++)
    {
        for(int row = 0; row < assignments[col].length; row++)

assignAvgArray[row] += assignments[row][col];

        assignAvgArray[col] = (assignAvgArray[col]/5);
    }

}

public String toString()
{
    String output = ("Student \t\t\t Marks \t\t\t\t Average \n" +
                    "Name \t\t\t\t out of 10 \t\t\t out of 10 \n" +
                    "\t\t A1 \t A2 \t A3 \t A4");
            for(int i = 0; i < assignments.length; i++)
            {
                output = output + "\n" + names[i];
                for(int col = 0; col < assignments[i].length; col++)
                {
                    output = output + "\t " + assignments[i][col] + "\t ";
                }
                output = output + "\t\t" + stuAvgArray[i];
                output = output + "\n" + assignAvgArray[i];
            }

    return output;
}

}

i've bolded where java says the error is coming from. I am trying to read in, store, and then calculate for the array spots [[(0,0),(1,0),(2,0),(3,0),(4,0)],[(0,1),(1,1),(2,1),(3,1),(4,1)..etc]]

what Im trying to ask is how can I create a loop that doesn't give me this exception, storing all the values of each column into separate spots, and dividing each number stored in the new array by 5 to give me the average.

if there's anything else I can do to help you understand my problem please let me know.

PS: This is what It's supposed to look like;

Student     Marks               Average
Name        out of 10           out of 10
        A1  A2  A3  A4  
Joe Smith   8   9   10  4   7.75
Tommy Jones     9   9   8   5   7.50
Sara Lee    0   10  10  9   7.25
Bob Fowler  10  9   9   0   7.00
Jane Doe    10  10  10  10  10.00
**Average   7.40    9.40    9.40    5.60**

I've calculated the row average but the bolded column average is giving me grief

Это было полезно?

Решение

The following line is wrong:

for(int row = 0; row < assignments[col].length; col++)

You are using row and increment col instead. As a result, you are doing an out of bound error in the block of that loop.

Replace it by:

for(int row = 0; row < assignments[col].length; row++)

But I'm afraid this is not the only problem. According to the way you read the input, assignments is a 2D-array with the row number for the first dimension and the column number for the second dimension. However, you are mixing the dimension here. As a reminder, you code is:

for (int col = 0; col < assignments.length; col++)
{
    for (int row = 0; row < assignments[col].length; row++) {
         assignAvgArray[row] += assignments[row][col];
    }

    assignAvgArray[col] = (assignAvgArray[col]/5);
}

As you can see, you are using col to identify the row. assignments.length is the number of lines.

for (int col = 0; col < assignments[0].length; col++)
{
    for (int row = 0; row < assignments.length; row++) {
         assignAvgArray[row] += assignments[row][col];
    }

    assignAvgArray[col] = (assignAvgArray[col]/5);
}

Другие советы

for (int col = 0; col < assignments.length; col++)
    {
        for(int row = 0; row < assignments[col].length; row++)
    assignAvgArray[row] += assignments[row][col];
        assignAvgArray[col] = (assignAvgArray[col]/5);
    }

}

assignments.length is 5 so col can become 4. assignAvgArray.length is 4. When you are giving the aassignAvgArray col=4 it will throw an arrayoutofboundException. fix it ,and if you still have an error comment under my answer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top