Tuesday, July 3, 2007

Read a Matrix from a text file using Java

Rules :
Rule 1 : The matrix text file values (contents of the file) should be space/tab limited
Rule 2 : The matrix text file should contain only integers
Rule 3 : The file should start and end with the matrix values, should not have anything else.


A sample matrix file content :

3 1 -5 -3 4
4 -5 -2 -3 -3
6 -8 -2 -6 1
3 4 1 -4 -8
1 4 -2 7 -1


Here goes the code :


private static int[][] getMatrix(String matrixFilePath) throws Exception
{
int matrix[][] = new int[1][0];

int charcount = 0;

File gf = new File(matrixFilePath);

FileInputStream fis = new FileInputStream(gf);

int ch=0, cl=0, ri=0; //char, columnlength, row index, currentcolumn


boolean negative = false;
boolean firstEntry = true;
boolean extraCharRead = false;
boolean blockEntered = false;

while(ch!=-1)
{
if(!extraCharRead)
{
ch = fis.read();
extraCharRead = false;
}

if(extraCharRead) { extraCharRead = false; System.out.println("EXTRA CHAR");}
else
//45 -, 9 tab, 32 space, 13 and 10 new line

if((ch==13)&&(charcount!=0))
if((ch = fis.read())==10) //new line
{
if(!(matrix[0].length == 0)) { ri++; }


if(firstEntry)
{
firstEntry=false;
cl = charcount;
}
else
{
if(!(cl == charcount))
{
System.out.println("MatrixFlip getMatrix() : File :"+matrixFilePath+" Line no : "+ri+" -- The number of columns doesn't match the first row. \n STOPPING PROGRAM !!!");
System.exit(0);
}
}
int tempmatrix[][] = matrix;
matrix = new int[ri+1][charcount];


for(int i=0;i<tempmatrix.length;i++)
for(int j=0;j<tempmatrix[0].length;j++)
{
matrix[i][j] = tempmatrix[i][j];
}

charcount = 0;
}

if(ch==45)
{
negative = true;
ch = fis.read();
if(!((ch > 47)&&(ch < 58))) {negative=false; extraCharRead = true;}
}

if((ch > 47)&&(ch < 58))
{
charcount ++;


int number = 0;
String numberString = "";

numberString += (char)ch;
ch = fis.read();

while((ch > 47)&&(ch < 58))
{
numberString += (char)ch;
ch = fis.read();
}
extraCharRead = true;

number = new Integer(numberString).intValue();

if (negative)
{
number = 0 - number;
negative = false;
}

if(firstEntry)
{
int tempmatrix[][] = matrix;
matrix = new int[1][charcount];


for(int j=0;j<tempmatrix[0].length;j++)
{
matrix[0][j] = tempmatrix[0][j];
}

matrix[0][charcount-1] = number;
}
else
{
matrix[ri][charcount-1] = number;
}
}
}

if(charcount == 0) // if a new line is inserted at the end of the file
{
int mh = matrix.length-1;
int tempmatrix[][] = matrix;
matrix = new int[mh][tempmatrix[0].length];


for(int i=0;i<mh;i++)
for(int j=0;j<tempmatrix[0].length;j++)
{
matrix[i][j] = tempmatrix[i][j];
}
}

System.out.println("\n PRINTING THE GRID MATRIX : ");

for(int i=0;i<matrix.length;i++)
{
System.out.println();

for(int j=0;j<matrix[0].length;j++)
{
System.out.print(matrix[i][j]+"\t");
}
}

return matrix;
}

2 comments:

Ali said...
This comment has been removed by the author.
X said...

I tired to use your matrix class but it reads incorrectly.. it seems to put the whole matrix in one row. for example an input of
1 2
3 4
is saved in a matrix as 1 2 3 4
im trying to fix it and hope u can help