Array out of Index in my code

import java.util.*;

class wavePrint {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int arr[][] = new int[n][n];
        int right = n - 1, left = 0, dir = 1, count = n*n, i, j;
        for (i = 0; i < n; i++) {
            for (j = 0; j < n; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        while (left <= right) {
            if (count > 0) {
                if (dir == 1) {
                    for (i = 0; i <= n - 1; i++) {
                        System.out.print(arr[i][left] + " ");
                        count--;
                    }
                    dir = 2;
                    left++;
                }
                if (dir == 2) {
                    for (i = n - 1; i >= 0; i--) {
                        System.out.print(arr[i][left] + " ");
                        count--;
                    }
                    dir = 1;
                    left++;
                }
            }
        }
    }
}

Showing this error :

1 1 1 2 2 2 3 3 3 Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at wavePrint.main(wavePrint.java:26)

coding blocks ide not working

@devfest2021_ca0dcbd15233fafb For Coding blocks IDE to work you have to name the class as “Main” not any other name. And the code that you sent is working fine is printing perfect wave print - column wise.

can you tell me whats wrong in the code

@devfest2021_ca0dcbd15233fafb As I said earlier the code is working fine for Column wise Wave print.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.