views:

18

answers:

2

Hi,

I'm doing my first steps with 2D arrays. Help needed on a practical example. Below code should store students and each of their results for 3 courses in a table. Below code returns an error. The error is caused by int studentResults[x][y] = sc.nextInt(); . Any ideas?

import java.util.*;

class CalcAverage 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);

        System.out.print("how many students?: ");
        int nbrOfStudents = sc.nextInt();

        int[][] studentResults ;

        studentResults = new int[nbrOfStudents][3] ;

        for (int x = 0 ; x < nbrOfStudents  ; x++)
        {
            System.out.println("Student " + (x + 1) + " : ");

            for (int y =0 ; y < 3 ; y++)
            {
                System.out.println("enter result course " + (y + 1) + " : ");
                int studentResults[x][y] = sc.nextInt();
            }
        }

    }// end main
}//end class
+1  A: 

Just say studentResults[x][y] = sc.nextInt(); with no int in front of it.

You only put the type in front of a variable name when you are first declaring it.

Charles Salvia
thank you Charles!
Baba
A: 

Make a dynamic array instead.

Karl