Question
Java I am having issues with completing this lab. The goal is to instead of randomly generating a matrix, is to instead to read a
Java
I am having issues with completing this lab. The goal is to instead of randomly generating a matrix, is to instead to read a matrix from a file whose name will be specified by the user.
I must have two integers before the matrix that donate the row and column size( I am not to use an n x n matrices), Assert that the column number and row number are both positive. Then, read in the matrix of characters. All character will be separated by a space, this you are guaranteed. And as mentioned as before print out the desired calculations to an output file whose name will be specified by the users.
I also have to handle the following errors/assertions that may occur:
- The user enters a file name for an input file that dose not exist.
-The user enters the name of an output that already exists.
- The file the user entered does not start with two ints.
- The two integers are negative.
- Find a character in the file that is not A or Z (ex: a random X).
Examples of Input/Output:
Input1.txt:
20 20 A Z A A Z A A Z A Z A Z Z A Z A A Z A A Z A A A A Z A Z A A A Z Z A Z Z A Z A Z Z Z Z A Z Z A A A Z A Z A Z A A A A A Z A Z A Z Z A A Z Z A Z Z A Z A Z Z A Z A Z A A A Z Z Z A A Z Z A A A Z Z A A A Z Z A Z Z A A Z A A A Z A Z Z Z A Z Z Z A A Z A Z A Z A A Z Z A Z Z A A Z Z A Z A Z Z Z Z A A A A Z Z Z Z Z A Z A A A A Z Z Z Z A A A Z A A A Z A A Z Z Z Z Z Z A Z A Z A A Z Z A A Z A A A A Z Z A Z A Z Z A A Z Z Z Z Z Z A Z A Z Z A A A Z A Z A A A Z Z Z Z Z Z Z Z A A Z Z Z A A Z A Z A A A Z A A A Z Z A Z A A Z Z A A A A A A Z A A A A Z A Z A Z Z A Z Z A Z A Z A A Z Z Z A Z Z Z A Z A A A Z A Z A A Z A A A A A Z A Z Z A Z Z Z Z A A Z A Z Z A A A A A Z Z Z A A A A Z Z A A Z A Z Z A A A A A A A A A A Z Z A A Z A Z A A Z A Z A Z Z A Z Z Z A A A A A Z Z A Z Z A A Z Z A A A A A Z A A Z A Z A A Z Z A Z
Output1.txt:
Row(s) with the most A's: 17 Column(s) with the most A's: 1, 2 , 3 , 18
My code so far:
public static void main(String[] args) throws FileNotFoundException{ //Declaring Variables. int X; int Y; // Create an Scanner Object. Scanner userInput = new Scanner(System.in); //Prompt for user to enter file name. System.out.print("Please, enter name of Input file: "); String inputMatrix = userInput.nextLine(); //Prompt for user to enter output file name. System.out.print("Please, enter name of Output file: "); String outputResult = userInput.nextLine(); userInput.close(); // File outputFile = new File(outputResult); // If Output File with name already exists, message will appear. if(outputFile.exists()){ System.out.println("Output file already exists. Please choose a different name."); return; } try{ File inputFile = new File(inputMatrix); Scanner matrixReader = new Scanner(inputFile); // Read in the matrix dimensions X = matrixReader.nextInt(); Y = matrixReader.nextInt(); // Check if dimensions are positive assert X > 0 && Y > 0 : "Matrix dimensions must be positive"; char[][] matrix = new char[X][Y]; // Read in the matrix values. for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { if (!matrixReader.hasNext()) { throw new IllegalArgumentException("File does not contain enough values"); } char value = matrixReader.next().charAt(0); // Check if value is valid (A or Z) assert value == 'A' || value == 'Z' : "Matrix value must be A or Z"; matrix[i][j] = value; } } matrixReader.close(); // Calculate both the row and column counts int[] RowCount = new int[X]; int[] ColumCount = new int[Y]; int maxRowCount = 0; int maxColumCount = 0; for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { if (matrix[i][j] == 'A') { RowCount[i]++; ColumCount[j]++; // Check if this row has more A's than the previous max if (RowCount[i] > maxRowCount) { maxRowCount = RowCount[i]; } // Check if this column has more A's than the previous max if (ColumCount[j] > maxColumCount) { maxColumCount = ColumCount[j]; } } } } // Write output to file // Display rows with the lagest number of A's. PrintWriter outputFileWriter = new PrintWriter(outputFile); outputFileWriter.println("Row(s) with the most A's:"); for (int i = 0; i < X; i++) { if (RowCount[i] == maxRowCount) { outputFileWriter.println(i+1); } } //Displays the Colum(s) with the largest number of A's. outputFileWriter.println("Column(s) with the most A's:"); for (int j = 0; j < Y; j++) { if (ColumCount[j] == maxColumCount) { outputFileWriter.println(j+1); } } outputFileWriter.close(); }catch(FileNotFoundException e){ System.out.println("Error: Input file not found."); }catch(IllegalArgumentException e){ System.out.println("Error: Reading input file: " + e.getMessage()); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started