source file name: matrix.java 

import java.io.*;

import java.util.*;

public class matrix

{

      public static void main(String[]args)throws IOException{

           int[][]dim=new int[2][2];

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

       System.out.println("Enter the rows of matrix:");

       dim[0][0]=Integer.parseInt(stdin.readLine());

       System.out.println("Enter the columns of matrix:");

       dim[0][1]=Integer.parseInt(stdin.readLine());

       int[][]A=new int[dim[0][0]][dim[0][1]];

       for(int i=0;i<A.length;i++)

       for(int j=0;j<A[i].length;j++){

    System.out.println("Enter element ("+(i+1)+","+(j+1)+"):");

     A[i][j]=Integer.parseInt(stdin.readLine());

     }

       System.out.println("Matrix");

       for(int i=0;i<A.length;i++)

{

             System.out.println();

             for(int j=0;j<A[i].length;j++)

{

System.out.print(A[i][j]+" ");

}

}


}

}

 

output:

D:\JavaPrograms\Misc>javac matrix.java


D:\JavaPrograms\Misc>java matrix

Enter the rows of matrix:

2

Enter the columns of matrix:

4

Enter element (1,1):

1

Enter element (1,2):

4

Enter element (1,3):

3

Enter element (1,4):

7

Enter element (2,1):

4

Enter element (2,2):

7

Enter element (2,3):

9

Enter element (2,4):

3

2D Matrix is

1 4 3 7

4 7 9 3