Getting the full answer but with an exception of ARRAY INDEX OUT OF BOUNDS. Please help

import java.util.*;
public class waveprintcolumn {

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

}

}

In eclipse it is showing at the first System.out.print() position.

Hi @ashish_mohanty,see your count is decreasing every time you print one number so do count-- inside the for loop in both the cases when dir==1 and dir==2.