Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to print matrix into file ?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to print matrix into file ?

    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("");
            }
        }
     
     
    }

    please help me.....
    thanks for your time to read this thread.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: how to print matrix into file ?

    Add a newline after the inner loop.

Similar Threads

  1. How do I print out the whole of my text file?
    By tangara in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 24th, 2013, 11:15 AM
  2. Print to txt file
    By anmason in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 8th, 2013, 09:19 AM
  3. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 01:20 PM
  4. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  5. Generating array (or arraylist) matrix from txt file
    By benjalizana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 14th, 2011, 08:09 PM