how to print the content of file in 2d matrix having same dimension as given in file(n*m).

how to print the content of file in 2d matrix having same dimension as given in file(n*m).

here is code:

import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mat1 { public static void main(String[] args) throws IOException { Scanner s = new Scanner(new File("rfg.txt")); List list = new ArrayList(); while (s.hasNextLine()) { String line[] = s.nextLine().split(" ");

     list.add(new double[] { Double.parseDouble(line[0]),Double.parseDouble(line[1]),Double.parseDouble(line[2]),Double.parseDouble(line[3]) });
}
    int numberOfRows = list.size();
    int numberOfColumns =4; 
    double[][] floatValues = new double[numberOfRows][numberOfColumns];
    for (int i = 0; i < numberOfRows; i++) {
        for(int j=0;j<numberOfColumns;j++){
        floatValues[i] = list.get(i);
        System.out.print(floatValues[i][j] + " "/* + floatValues[i][1] + " " + floatValues[i][2]*/);

        }
        System.out.println();
    }

}}

here is .txt file:

1.0 3.1 2.1 3.4 2.4 2.0 4.6 5.1 0.2 3.3 4.7 9.1 4.0 5.4 5.1 3.2 6.1 2.1 6.4 2.6

here in program i have to specify no. of columns every time. it is not the case. but i want to print the file of any dimensions in the form of 2d n*m matrix. pls help me

View Answers

May 2, 2011 at 3:01 PM

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class mat1 {
    public static void main(String[] args) throws IOException {
    Scanner s = new Scanner(new File("rfg.txt"));

        List<double[]> list = new ArrayList<double[]>();

        while (s.hasNextLine()) {
            String[] line = s.nextLine().split(" ");
            list.add(new double[] { Double.parseDouble(line[0]), Double.parseDouble(line[1]), Double.parseDouble(line[2]), Double.parseDouble(line[3]) });
        }

        int numberOfRows = list.size();
        int numberOfColumns = 4;
        double[][] doubleValues = new double[numberOfRows][numberOfColumns];
        for (int i=0 ; i < doubleValues.length ; i++)
        {    doubleValues[i] = list.get(i);  
            System.out.println();
            for  (int j=0 ; j < doubleValues[i].length ; j++){
                System.out.print(doubleValues[i][j]+" ");
                  }
        }
    }
}









Related Tutorials/Questions & Answers:
how to print the content of file in 2d matrix having same dimension as given in file(n*m).
how to find subarray from given large 2d array of arbritary dimension
Advertisements
how to change file from .txt to .mat(matrix)
how to modify content of XML file
How to pretty print a JSON file in Python?
how to display the excel file content in the jsp
how to distinguish engines having same code - Struts
how to print the average of each column of 2d n*m array using java
how read data from doc file in same formate in jsp
How to write content of one file to another file.
how to print fasta file into jtable using netbeans IDE
Maven dependency for net.sf.m-m-m - mmm-content-parser-impl-pdf version 1.0.0 is released. Learn to use mmm-content-parser-impl-pdf version 1.0.0 in Maven based Java projects
How to browse and upload the file in same page using jsp.
How to browse and upload the file in same page using jsp.
changing the file content
How to write content of one file to another file and calculate checksum for accuracy.
Read text file to 2D array and sorting the second column
Java File content type
How to delete content of a log file from linux terminal?
openning the pdf or doc file in same jsp
ModuleNotFoundError: No module named 'miu-print-file'
How to copy many files into one file and how to manage the content of the final file?
How to copy a file in java
how to add the two tables in same row when generating pdf file from jsp - JSP-Servlet
Determining if two Filename paths refer to the same file.
Retrieving Data From the XML file
How to read and compare content of two different text file
Java display file content in hexadecimal format
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.7 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.7 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.3.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.3.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.3.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.3.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.4.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.4.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.9.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.9.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.8.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.8.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.6 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.6 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.5 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.5 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.4 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.4 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.2.3 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.2.3 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.0.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.0.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 4.0.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 4.0.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.8 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.8 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.6 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.6 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.5 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.5 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.4 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.4 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.3 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.3 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.2 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.2 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.1 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.1 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 3.0.0 is released. Learn to use n2o-platform-starter-jaxrs-server version 3.0.0 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 2.25 is released. Learn to use n2o-platform-starter-jaxrs-server version 2.25 in Maven based Java projects
Maven dependency for net.n2oapp.platform - n2o-platform-starter-jaxrs-server version 2.24 is released. Learn to use n2o-platform-starter-jaxrs-server version 2.24 in Maven based Java projects

Ads