Getting two run errors and two correct answers for test cases

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; //BOTTOM = 3
	int dir=1;
	while(count>0) {
		
		if(dir==1) {
			for(int i=top;i<bottom;i++) {
				System.out.print(arr[i][left]+","+" ");
				count--;
			}
			left++;
			
			dir=2;
		}
		if(dir==2) {
			for(int j=bottom-1;j>=top;j--) {
				System.out.print(arr[j][left]+","+" ");
				count--;
			}
			left++;
			
			dir=1;
		}
		
	}
	System.out.print("END");

}

}

Hi,see when you give an input of 1X1 matrix then your code doesn’t work.So for that every time you have to check count every time you get out of one loop so in the if conditions where you check dir also put a condition for count ie if(dir==1&&count>0).Similarly for second if condition also. if(dir==2&&count>0).

Hi,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.

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.