Hi all...

I need some help in manipulation of the text files to read in a matrix text file. The format of the text file is as follows:

<matrix>
rows = 2
cols = 2

1 2
2 4
</matrix>

I need to read in the text file and put the 1 2 2 4 into a 2D array. I need some help in reading the text file and populate the 2D array.
and this is my code segment:

String file1 = "", file2 = "";
        int cols, rows;
        double[][] matrix;

        file1 = readFile();
        try {
            BufferedReader brStream = new BufferedReader(new FileReader(file1));

            String strLine = "";
            while (strLine.equals(brStream.readLine().equals("</martrix>"))) {
               if(strLine.contains("rows = ")){
                   Scanner str = new Scanner(strLine);
                   str.useDelimiter(" = ");
                   while(str.hasNextToken)
               }
            }

is there any suggestion reading in the file? thanks!!

the file has only one tag like this

<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>

or

/////////////////////////////////////////////
<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>


<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>


<matrix>
rows = 2
cols = 2
1 2
2 4
</matrix>

/////////////////////////EOF//////////

1.READ  YOUR FILE UNTIL YOU FIND <MATRIX>...


2.while (strLine.equals(brStream.readLine().equals("</martrix>"))) {


3.AGAIN READ LINE UNTIL FIND "ROW"


4.IF IT IS THEN READ LINE "row=2" and assign it to string


5.after split that string like string arr[]= string.split("=");


6.get int row=(integer)arr[arr.lenth-1];


7. do it same for column int col.


8.then initiate 2d array like int arr[][]=new int[row][col];


9.then read every line you get 1  2
3  4 and assign to string and use split and iterate arr[][] and and store splitted string into arr[][]...


}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.