writing to file with new arrays java -
i need multiplying 2d arrays , input file (input.txt) , need writing multiplied outputs , output file(output.txt) why isn't 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 question 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 2 matrixes int[][] matrix3 = new int[matrix1.length][matrix2.length];// takes lengths matrix1&&matrix2 for(int =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 assuming file won't contain more 100 lines, drawback while ((line = bufferedreader.readline()) != null){ system.out.println(line);//to check code reading file line line if(!line.trim().equals("")){//we ignore blank lines words = line.trim().split("\\s+"); //split line in words separated 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 blank lines words=line.trim().split("\\s+"); (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 blank lines words=line.trim().split("\\s+"); (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[][]); } }
check similar question how write array file java , http://www.java2s.com/tutorial/java/0180__file/usefilewritertowriteanarrayofstringstoafile.htm
Comments
Post a Comment