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

Ads
 

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 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
How to copy a file in java
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?
Java File content type
changing the file content
how to save an input to jsp page in a text file?ave an input given b
openning the pdf or doc file in same jsp
ModuleNotFoundError: No module named 'miu-print-file'
How to read content of JPEG file in Java?
How to Read a File in Java
how to convert .xml file to csv file
How to write in File in Java
How to rename a file in Java?
ModuleNotFoundError: No module named 'prometheus-file-content-exporter'
how to convert a jar file into .exe file
how to convert image file to text file?
how to add a file in GZIP
How to write in File in Java
How to create a jar file
How to prepare XML file?
File

Ads