Reputation: 101
So i have been stuck with this problem for a week now.
I need to get the values from the last of column of the file that i am reading. But i when i try to read that array outside the while loop i am getting zeroes instead of the values.
But if i read the same array inside the while loop, i get the values perfectly fine.
BufferedReader br2 = new BufferedReader(new FileReader("/home/manofsteel/yeast_training.txt"));
// Starting to read a file line by line.
while((line = br2.readLine()) != null)
{
row = 0;
String[] valsNew = line.trim().split("\\s+"); /* Getting rid of all
spaces since the file
contains a lot of them. */
cla = new int[lines];
cla[row] = Integer.parseInt(valsNew[8]); /* 8 because i need the
values from last column. */
row++;
}
for(int i=0;i<cla.length;i++)
{
// Trying to print back that array.
System.out.println("x"+cla[i]);
}
}
The output i am getting is
x0
x0
x0
x0
The output i want is
x4
x1
x1
x1
x2
x7
x2
x7
Any suggestions are welcome.
If you think i need to share the input file. Do let me know.
Thanks.
Upvotes: 1
Views: 134
Reputation: 491
I don't think this can ever work since the Array you're using to store the data in is always re-initialized in every iteration:
cla = new int[lines];
And also, I'm not sure if you always want to set your row to zero each time you execute a new iteration (since at the end you're doing row++;) :
row = 0;
Upvotes: 2