Q:

Exception in thread main error when trying to fill in a 2D array in column major form java

Code:
import java.util.*;

public class lab3  {
    public static void getScores(int[][] matrix) { 
        Scanner kb = new Scanner(System.in);
        int r, c;
        for (c = 0; c < matrix[0].length; c++){
           for (r = 0; r < matrix.length; r++) 
               System.out.println("enter scores for test "+(c+1)+ " and student "+(r+1));
            matrix[r][c] = kb.nextInt(); 
        } 
        kb.close();
    }
    public static void printArray(int[][] matrix) { 
        for (int c = 0; c < matrix[0].length; c++)
            for (int r = 0; r < matrix.length; r++){ 
                System.out.printf("%5d", matrix[r][c]); 
                System.out.println(); 
            } 
        }
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        System.out.println("how many students are there?");
        int students=kb.nextInt();
        System.out.println("How many tests are there?");
        int tests=kb.nextInt();
        int [][] grades= new int [students][tests+1];

        getScores(grades); 
        printArray(grades);

I feel like I'm close but I keep getting an error. filling an array in column major form is the main deal so "get scores" method and the declared array at the bottom is where I think my problem is please help me 

A:

In your getScores method, inner for loop one with r as counter is missing braces, correct code should be something, if I got correctly what you want to do,
   for (r = 0; r < matrix.length; r++) {
    System.out.println("enter scores for test "+(c+1)+ " and student "+(r+1));
    matrix[r][c] = kb.nextInt();
   }

Also, my understanding is in this line of code it should be just tests and not     tests + 1
int [][] grades= new int [students][tests+1];

And moreover mostly it is much better to answer your queries when you specify what exception you are getting and at which line, so users in SO don't have to actually execute your code to see what issue you are facing.
Hope this helps!!

