Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how to print a matrix into File.

  Asked By: Vivek    Date: Jun 16    Category: Java    Views: 613
  

Hi everyone,
I want to know that how to write a matrix into text file?
actually here i am trying to write a program which take no of rows and columns from user and also element of matrix and then i want to store that matrix into a separate file(abc.txt) and it's Transpose result
want to store in separate file(xyz.txt). for this i am stuck in, i am not getting idea that how to save my matrix into a file...
i have been searched regarding of my this problem but did not find any clue . let me post my code whatever effort i did to do...


public class MyTransposeOfMatrix {
public static void main(String[] args) throws FileNotFoundException {


PrintWriter pw =new PrintWriter("D:/SCJP Sun/netbeans/scjp practice/src/com/scjp/braintraser/xyz.txt");
int rows, columns;
System.out.println("Please enter no of rows:");
Scanner sc=new Scanner(System.in);
rows=sc.nextInt();
System.out.println("Please enter no of columns:");
columns=sc.nextInt();
int matrix[][]=new int[rows][columns];
int tmatrix[][]=new int[columns][rows];
System.out.println("please enter element for matrix:");
for(int i=0;i<rows;i++){
for(int j=0;j<columns;j++){
matrix[i][j]=sc.nextInt();
}
}

for(int i=0;i<rows;i++){
for(int j=0; j<columns; j++){
tmatrix[i][j]=matrix[j][i];
}
}
System.out.println("The Matrix is:");
for(int i=0; i<rows; i++){
// pw.print(i);
for(int j=0; j<columns; j++){
// System.out.print(matrix[i][j]+" ");

pw.print(matrix[i][j]); // here trying to print into file but it's printing in single line
}
System.out.println("");
}
pw.flush();
System.out.println("The Transpose of Matrix is: ");
for(int i=0; i<columns; i++){
for(int j=0; j<rows; j++){
System.out.print(tmatrix[i][j]+" ");
}
System.out.println("");
}
}


}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on how to print a matrix into File. Or get search suggestion and latest updates.




Tagged: