I need help multiplying 2d arrays from and input file (input.txt) and I need help writing their multiplied outputs to and output file(output.txt) Why isn't my method showNewArray working?
- input.txt:
- 9 3
- 8 5
- 2 3 3 8
- 2 1 1 2
- 3 3 5 9
- 2 2 3
- 3 4 6
- 8 9 7 6
- (edit: sorry wrote the question really fast!)
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class hw1 {
- public int[][] showNewArray(int[][] matrix1, int[][] matrix2){// mulitplies two matrixes together
- int[][] matrix3 = new int[matrix1.length][matrix2.length];// takes the lengths from matrix1&&matrix2
- for(int i =0;i <matrix1.length;i++){
- for(int j =0; j<matrix2[0].length;j++){//column length of matrix2
- for(int k =0; k< matrix1[0].length;k++){//column length of matrix1
- matrix3[i][j] += matrix1[i][k] * matrix2[k][j];// multiplies em
- }
- }
- }
- showNewArray(matrix3[][]);
- }
- public static void main(String args[]){
- FileReader fileReader = null;
- FileWriter fileWriter = null;
- BufferedReader bufferedReader = null;
- try {
- fileReader = new FileReader("C:/javastuff/principles/input.txt");
- fileWriter = new FileWriter("C:/javastuff/principles/output.txt",true);
- bufferedReader = new BufferedReader(fileReader);
- int flag=0;
- String line = null;//initialization
- String words[];
- int lineCount = 0;
- int row1=0, column1=0, row2=0, column2=0;
- String fileData[] = new String[100];//we are assuming the file won't contain more than 100 lines, but this is clearly a drawback
- while ((line = bufferedReader.readLine()) != null){
- System.out.println(line);//to check that code is reading file line by line
- if(!line.trim().equals("")){//we ignore the blank lines
- words = line.trim().split("\\s+"); //split the line in words separated by spaces
- if(lineCount==0){
- row1 = Integer.parseInt(words[0]);
- column1 = Integer.parseInt(words[1]);
- }
- else if(lineCount==1){
- row2 = Integer.parseInt(words[0]);
- column2 = Integer.parseInt(words[1]);
- if(column1!=row2){
- flag = 1;
- break;
- }
- }
- else{
- fileData[lineCount] = line;
- }
- lineCount++;
- }
- }
- if(flag==1){
- System.out.println("Invalid input.");
- fileWriter.write("Invalid input.");
- }else{
- int[][] matrix1=new int[row1][column1];
- int[][] matrix2=new int[row2][column2];
- int fileRow=0;
- for(int index=2;index<row1+2;index++) {
- line = fileData[index];
- if(!line.trim().equals("")){//we ignore the blank lines
- words=line.trim().split("\\s+");
- for (int col = 0; col < words.length; col++) {
- matrix1[fileRow][col] = Integer.parseInt(words[col]);
- }
- fileRow++;
- }
- }
- System.out.println("matrix1:");
- fileWriter.write("\nmatrix1:\n");
- for(int p =0;p<row1;p++){
- for(int q =0;q<column1;q++){
- System.out.print(matrix1[p][q]+" ");
- fileWriter.write(matrix1[p][q]+" ");
- }
- System.out.println();
- fileWriter.write("\n");
- }
- fileRow=0;
- for(int index=row1+2;index<row2+row1+2;index++) {
- line = fileData[index];
- if(!line.trim().equals("")){//we ignore the blank lines
- words=line.trim().split("\\s+");
- for (int col2 = 0; col2 < words.length; col2++) {
- matrix2[fileRow][col2] = Integer.parseInt(words[col2]);
- }
- fileRow++;
- }
- }
- System.out.println("matrix2:");
- fileWriter.write("\nmatrix2:\n");
- for(int p =0;p<row2;p++){
- for(int q =0;q<column2;q++){
- System.out.print(matrix2[p][q]+" ");
- fileWriter.write(matrix2[p][q]+" ");
- }
- System.out.println();
- fileWriter.write("\n");
- }
- }
- bufferedReader.close();
- fileReader.close();
- fileWriter.close();
- }
- catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- showNewArray MpliedTogether = new showNewArray(matrix1[][], matrix2[][]);
- }
- }
