2016-02-05 51 views
0

我需要將來自和輸入文件(input.txt)的2d數組相乘的幫助,我需要幫助將相乘的輸出寫入輸出文件(output.txt)爲什麼我的方法showNewArray不工作?使用新數組寫入文件java

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

請訪問[問]頁和[編輯]你的問題。我們需要一個具體的問題,只需要足夠的代碼來理解錯誤,輸出與期望的輸出,以幫助我們知道如何解決錯誤。 – Spencer4134

+0

另請注意格式化。你可以看到有些數字已經從你的文件中「脫落」了,很難理解這些數組是什麼。 –

回答