import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

class Mar13 {
	public static void main(String[] args) {

		/* A 2D array is an array of *** arrays *** */

		// Here are some arrays

		int[] arr1 = {1,2,3};
		int[] arr2 = {4,5,6};
		int[] arr3 = {7,8,9};

		// Here is an array of arrays

		int[][] matrix = {arr1, arr2, arr3};

		// Lets print the values in matrix, with each row
		// on a separate line

		for(int i = 0; i < matrix.length; i++) {
			// get one of the arrays, the one at index i
			int[] arr = matrix[i];
			// print the elements in arr
			for(int j = 0; j < arr.length; j++) {
				System.out.print(arr[j] + " ");
			}
			System.out.println();
		}

		// Printing the values in a more condensed code
		// See below

		printMatrix(matrix);

		// Populate the 2D array with values entered by the keyboard

		Scanner kb = new Scanner(System.in);

		System.out.println("Enter 9 integers");
	
		for(int i = 0; i < matrix.length; i++) {
			for(int j = 0; j < matrix[i].length; j++) {
				matrix[i][j] = kb.nextInt();
			}
		}

		// print the values to the console

		printMatrix(matrix);

		// Populate a new 2D array with values in matrix.txt

		Scanner fin = null;
		try {
			fin = new Scanner(new File("matrix.txt"));
		}
		catch(FileNotFoundException e) {
			System.out.println("File not found");
			return;
		}

		// Change the delimiters to commas and \n characters
		fin.useDelimiter(",|\n");

		// Read the number of rows and columns
		int numRows = fin.nextInt();
		int numCols = fin.nextInt();

		// allocate new 2D array
		int[][] matrix2 = new int[numRows][numCols];

		for(int i = 0; i < numRows; i++) {
			for(int j = 0; j < numCols; j++) {
				matrix2[i][j] = fin.nextInt();
			}
		}

		printMatrix(matrix2);


	}

	static void printMatrix(int[][] m) {
		for(int i = 0; i < m.length; i++) {
			for(int j = 0; j < m[i].length; j++) {
				System.out.print(m[i][j] + " ");
			}
			System.out.println();
		}
	}
}
