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 print the average of each column of 2d n*m array using java
how to distinguish engines having same code - Struts
how read data from doc file in same formate in jsp
how to display the excel file content in the jsp
how to print fasta file into jtable using netbeans IDE
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 write content of one file to another 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 delete content of a log file from linux terminal?
Java File content type
changing the file content
Java display file content in hexadecimal format
Read text file to 2D array and sorting the second column
How to write content of one file to another file and calculate checksum for accuracy.
how to add the two tables in same row when generating pdf file from jsp - JSP-Servlet
Retrieving Data From the XML file
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
How to read and compare content of two different text file
How to read content of JPEG file in Java?
Determining if two Filename paths refer to the same file.
how to write a function to print for finding the longest palindrome in the given string
how can i print the selected content of a frame
How to print different Messge for the same NumberFormatException on different cause objects in JAVA?
Print pdf file in JavaScript - IE and Chrome

Ads