user1347525
user1347525

Reputation: 1

Matrix from File

I am trying to create a matrix from a text file. The problem is that when the Buffered Reader function readline() is done parsing first line of file it comes to second line but the its reading it as empty which it is not.

void covar()
    {
        double [][]covar=new double[10][5];
        int i=0;
        int j=0;
        try
        {
            FileInputStream fstream = new FileInputStream("class 1\\feature_vector.txt");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String input;

            while((input=br.readLine())!= null)
            {               
                String [] temp=input.split(",");
                //System.out.println(input.split(",").length);
                covar[i][j]= new Double(temp[0]);
                covar[i+1][j]=new Double(temp[1]);
                covar[i+2][j]=new Double(temp[2]);
                covar[i+3][j]=new Double(temp[3]);
                covar[i+4][j]=new Double(temp[4]);
                //i=0;
                j++;
            }

            in.close();
        }
        catch(Exception e)

        {
            e.printStackTrace();

        }

Above is the code. The file name is perfect and nothing is wrong with the stream thing. Can you guys help me out with what is wrong with this.

Here is the content of the file:

0.75,321.0,0.22429906,0.97507787,1.966202512778112
0.33333334,135.0,-0.014814815,1.0,5.323770568766052
0.64285713,311.0,0.025723472,1.0,4.764298570227433
0.6,188.0,0.03723404,1.0,4.7349608150168105
0.25,189.0,0.16931216,0.98941797,7.15681209803803
0.71428573,194.0,-0.26804122,0.96391755,5.1654456838422425
0.6,173.0,0.028901733,1.0,6.54275787030257
0.2857143,257.0,0.031128405,1.0,6.095356508899233
0.23076923,197.0,-0.04568528,1.0,3.784908227189768
0.18181819,231.0,0.17316018,0.987013,5.956322938602553

Upvotes: 0

Views: 272

Answers (3)

trutheality
trutheality

Reputation: 23455

Your file might have some strange line-terminators that are making the reader think there is an extra line.

You can try to just make your code skip blank lines:

while((input=br.readLine())!= null) {
    if( input.length() > 0 ){
        String [] temp=input.split(",");
        for (int i = 0 ; i != 5 ; i++) {
            covar[j][i] = new Double(temp[i]);
        }
    }
    ++j;
}

Upvotes: 0

neevek
neevek

Reputation: 12128

It looks like you are using the wrong indicies for you matrix, I think it should be something like this:

int i = 0;
while((input=br.readLine())!= null) {               
    String [] temp=input.split(",");
    //System.out.println(input.split(",").length);
    covar[i][0]= new Double(temp[0]);
    covar[i][1]=new Double(temp[1]);
    covar[i][2]=new Double(temp[2]);
    covar[i][3]=new Double(temp[3]);
    covar[i][4]=new Double(temp[4]);
    ++i;
}

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

There are two things that are obviously wrong:

  • You do not need variable i, because one of the dimensions is fixed, and you "unrolled" the loop five times
  • You swapped the indexes: j should go first, that's the one changing from 0 to 9.

For example:

String [] temp=input.split(",");
covar[j][0] = new Double(temp[0]);
covar[j][1] =new Double(temp[1]);
covar[j][2] =new Double(temp[2]);
covar[j][3] =new Double(temp[3]);
covar[j][4] =new Double(temp[4]);

You could put the loop back to shorten your code:

String [] temp=input.split(",");
for (int i = 0 ; i != 5 ; i++) {
    covar[j][i] = new Double(temp[i]);
}

Upvotes: 1

Related Questions