// This program reads an integer matrix and prints it out. // It prompts the user for the file name, then reads the file. // The program assumes that the dimensions of the matrix are // given by the first two values as number of rows, number of columns. import java.io.*; import java.util.*; public class Matrix { public static void main(String[] args) throws FileNotFoundException { System.out.println("This program will process a series"); System.out.println("of numbers in the form of a matrix from a file."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("What is the file name? "); String name = console.nextLine(); Scanner input = new Scanner(new File(name)); int[][] m = readMatrix(input); printMatrix(m); } static int[][] readMatrix(Scanner input) { int numRows; int numCols; int [][] matrix; numRows = input.nextInt(); numCols = input.nextInt(); System.out.println(numRows + " rows to read"); System.out.println(numCols + " cols to read"); matrix = new int[numRows][numCols]; for (int row = 0; row < numRows; row++) { for (int col = 0; col < numCols; col++) { matrix[row][col] = input.nextInt(); } } return matrix; } static void printMatrix(int [][] m){ for (int r = 0; r < m.length; r++) { for (int c = 0; c < m[r].length; c++) { System.out.print(m[r][c] + " "); } System.out.println(); } } }