【发布时间】:2017-02-08 10:16:30
【问题描述】:
所以我已经被这个问题困扰了一个星期了。
我需要从我正在阅读的文件的最后一列中获取值。 但是当我尝试在 while 循环之外读取该数组时,我得到的是零而不是值。
但如果我在 while 循环中读取相同的数组,我会得到非常好的值。
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]);
}
}
我得到的输出是
x0
x0
x0
x0
我想要的输出是
x4
x1
x1
x1
x2
x7
x2
x7
欢迎提出任何建议。
如果您认为我需要共享输入文件。请告诉我。
谢谢。
【问题讨论】:
-
你应该在while循环上方移动
cla = new int[lines];行 -
在每次迭代中,您都会用一个新数组(仅包含 0)覆盖您的数组:
cla = new int[lines];。 -
还有
row=0上面的while循环
标签: java machine-learning integer bufferedreader filereader