用新数组java写入文件

时间:2016-02-05 20:57:18

标签: java arrays

我需要帮助将2d数组与输入文件(input.txt)相乘,我需要帮助将其相乘的输出写入并输出文件(output.txt)为什么我的方法showNewArray不工作?

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

(编辑:抱歉,写得很快!)

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[][]);
    }
}