Вопрос

I am trying to create a program that stores student test scores.

Each student has 4 test scores that must be stored in a 2D array. The JSwing App will allow users to enter 4 test marks and once they click a button it will add the 4 marks to the array.

I have tried to figure out how to do it by I keep getting arrayindexboundexception. Also, I am not sure if I am even doing it correctly.

This is all done in JSwing by the way.

public class StudentGradesView extends FrameView {
    final static int students = 15;
    final static int numOfTest = 4;

    //DELCARING THE 2D ARRAY
    double[][] marks = new double[students][numOfTest];

    public void addInfo() {
        double testScores[] = new double[4];
        String test1, test2, test3, test4;
        double score1, score2, score3, score4;
        int i = 0;
        test1 = testOneIn.getText();
        test2 = testTwoIn.getText();
        test3 = testThreeIn.getText();
        test4 = testFourIn.getText();
        score1 = Double.parseDouble(test1);
        score2 = Double.parseDouble(test2);
        score3 = Double.parseDouble(test3);
        score4 = Double.parseDouble(test4);
        testScores[0] = score1;
        testScores[1] = score2;
        testScores[2] = score3;
        testScores[3] = score4;

        //First for loop starts at in the first row and automatically
        //increments to the next row after the first rows columns are filled
        for (int row = 0; row < marks.length; row++) {

            //Second loop automatically cycles through each column in the
            //current row adding test scores. After this loop finishes
            //the program returns to the first loop and advances to the next 
            //row, at which point it will fill that row with the same test data.
            for (int col = 0; col < 4; col++) {
                marks[row][col] = testScores[i];
                i++;
            }
        }
    }

}

This is what I have so far. Any help is appreciated.

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

Решение 3

I'm not sure if I should be adding a second answer, but this is how it should look

public class StudentGradesView extends FrameView {
 final static int students=15;
 final static int numOfTest=4;

 //Start with the first student in the 2D array
 private int row = 0;


//DELCARING THE 2D ARRAY
double[][] marks = new double[students][numOfTest];


public void addInfo(){
    double testScores[]=new double[4];
    testScores[0]=Double.parseDouble(testOneIn.getText());
    testScores[1]=Double.parseDouble(testTwoIn.getText());
    testScores[2]=Double.parseDouble(testThreeIn.getText());
    testScores[3]=Double.parseDouble(testFourIn.getText());

    //Add all four test scores to the current student
        for(int col=0;col < testScores.length; col++){
            marks[row][col]= testScores[col];
            }

    //Advance to the next student
    row++;
   }
  }

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

For your original problem

for(int row=0; row <marks.length; row++){
        for(int col=0;col <4; col++){
            marks[row][col]= testScores[i];
            i++;
        }

Is incrementing i beyond the length of testScores. Use this

for(int row=0; row <marks.length; row++){
            i = 0;
            for(int col=0;col <4; col++){
                marks[row][col]= testScores[i];
                i++;
         }

FWIW, you can also reduce the amount of code you have in this section, the number of variables, and ultimately the amount of memory allocated for variables:

    test1=testOneIn.getText();
    test2=testTwoIn.getText();
    test3=testThreeIn.getText();
    test4=testFourIn.getText();
    score1=Double.parseDouble(test1);
    score2=Double.parseDouble(test2);
    score3=Double.parseDouble(test3);
    score4=Double.parseDouble(test4);
    testScores[0]=score1;
    testScores[1]=score2;
    testScores[2]=score3;
    testScores[3]=score4;

by doing something like this

    testScores[0] = Double.parseDouble(testOneIn.getText());

You are incrementing i beyond the bounds of your testScores array. The array only has a length of 4. The second iteration of marks would cause i > testScores.length giving you the exception.

Consider updating your input to accept the student number:

int student = Integer.parseInt(studentIn.getText());

Then you could apply the test score input to that student only:

for (int col = 0, i = 0; col < 4; col++, i++) {
    marks[student][col] = testScores[i]; 
}

Also since you are not doing a double for loop the i variable isn't necessary you could use col instead:

for (int col = 0; col < 4; col++) {
    marks[student][col] = testScores[col]; 
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top