Question
Please, I need help reading from the file in the following format: 5 (this first number determines the dimension of the matrix 5X5) . This can be any number. The other numbers fill the squar 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Also need to provide recovery in the case the file is not found or mispelled. Because the file is being passed through the command line. import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.NoSuchElementException; import java.util.Scanner; import java.util.StringTokenizer; public class MagicSquareTester { public static void main(String[] args) throws NoSuchElementException { // This code takes an argument from the command line in order to determine // what file to scan for squares to perform. BufferedReader br = null; try { br = new BufferedReader(new FileReader(args[0])); } catch (FileNotFoundException fnfe) { System.out.println("Unable to locate your file. Please try again."); System.exit(0); } // end command line code Scanner inFile = new Scanner(br); Scanner sizeScan; String line; int n, i, j; // Starts taking in entries from the file while (inFile.hasNextInt()) { line = inFile.nextLine(); sizeScan = new Scanner(line); n = sizeScan.nextInt(); int[][] square = new int[n][n]; // int numbers = sizeScan.nextInt(); // System.out.println(numbers); //Prompt user for input //Scanner userInput = new Scanner(System.in); //Determines the size of the square matrix //System.out.print(" Please enter the size of the square matrix you wish to test: "); //n = userInput.nextInt(); //square = new int[n][n]; //Fills in the square with integers //System.out.println(" Please enter " + n*n + " integers(from 1 to " + n*n + ") to fill the " + n + "X" + n + " matrix: "); for (i = 0; i < n; i++) { StringTokenizer st; try { st = new StringTokenizer(br.readLine()); for (j = 0; j < n; j++) { //square[i][j] = numScan.nextInt(); square[i][j] = Integer.parseInt(st.nextToken()); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //Creates a new Square of the given size MagicSquare s = new MagicSquare(square); System.out.println(square); //Displays the values entered for the matrix System.out.println(" *************** Square *************** "); System.out.println(s); //Prints the row sum System.out.println("The row sum is: " + s.rowSum() + " "); //print the sums of its columns System.out.println("The column sum is: " + s.colSum() + " "); //print the sum of the main diagonal if the sum of the two diagonals are thesame System.out.println("The sum of the main diagonal is: " + s.diagSum() + " "); //determine and print whether it is a magic square System.out.println("Is it a magic square: " + s.isMagicSquare()); } } }
Explanation / Answer
package com.cramster.nov16; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.NoSuchElementException; import java.util.Scanner; public class MagicSquareTester { public static void main(String[] args) throws NoSuchElementException { // This code takes an argument from the command line in order to determine // what file to scan for squares to perform. BufferedReader br = null; while(true) { try { Scanner console = new Scanner(System.in); System.out.printf("Enter the File Name : "); String fileName = console.nextLine(); br = new BufferedReader(new FileReader(fileName)); break; } catch (FileNotFoundException fnfe) { System.out.println("Unable to locate file. Please try again."); } } Scanner inFile = new Scanner(br); int matrixSize = inFile.nextInt(); int[][] matrix = new int[matrixSize][matrixSize]; int row = 0; int column = 0; int count = 0; while(inFile.hasNext() && row = matrixSize) { row++; column = 0; } } if(inFile.hasNext() || count != matrixSize * matrixSize) { System.err.println("File is not in correct Format. Terminating program."); System.exit(1); } System.out.printf("The matrix is : "); printMatrix(matrix); } public static void printMatrix(int[][] m) { for(int[] row : m) { for(int element : row) System.out.printf("%d ", element); System.out.println(); } } }