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

Ads









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 read data from doc file in same formate in jsp
how to print the average of each column of 2d n*m array using java
How to browse and upload the file in same page using jsp.
How to browse and upload the file in same page using jsp.
how to print fasta file into jtable using netbeans IDE
changing the file content
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 copy many files into one file and how to manage the content of the final file?
How to delete content of a log file from linux terminal?
How to copy a file in java
openning the pdf or doc file in same jsp
Java File content type
How to read content of JPEG file in Java?
ModuleNotFoundError: No module named 'miu-print-file'
Determining if two Filename paths refer to the same file.
how to save an input to jsp page in a text file?ave an input given b
Retrieving Data From the XML file
Print pdf file in JavaScript - IE and Chrome
how to write a function to print for finding the longest palindrome in the given string
ModuleNotFoundError: No module named 'prometheus-file-content-exporter'
Print pdf file in JavaScript - IE and Chrome
FILE
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?
File
file

Ads