我有这个功能,它被设想为打开一个文件并读取第一个元素[1st row] [1st column],将其存储在一个3D数组[文件索引] [行] [列],然后打开下一个文件并执行相同的过程。此操作应该一直运行,直到所有文件中的所有行和列都存储在3D数组中。 我的问题是,这个函数做的工作,但它只读取每个文件的第一行(行),并停在那里! 任何专家都可以帮我弄清楚我的功能正在发生什么?我一直在使用Java进行约两一个月了:/java:一次读取所有csv文件并总结元素/行
public static double[][][] fetch_data(int start, int end, int column) throws IOException{
// start and end are for files start and end index.
int length = file_length(start); // since all files have the same length. The file_length() function just determine the length.
data = new double [number_of_files][length][number_of_columns];
int sum = 0;
for (int i = 0; i < length; i++){
for (int index = start; index < end; index++){
Scanner scan = new Scanner (new BufferedReader(new FileReader(file_call(index)))); // file_call() is a function that lookup the file dir.
scan.nextLine();
String scanedData = scan.nextLine();
String [] array = scanedData.split(",");
data[index][i][colum] = Double.parseDouble(array[index]);
sum += data[index][i][colum];
System.out.println(" element " + data[index][i][column]); // just to test the loop
}
}
return data;
}
的数据文件看起来像: 文件(1):
A, B, C, D
1, 2, 3, 4
13, 2, 3, 4
1, 2, 3, 4
...
文件(2):
A, B, C, D
5, 6, 7, 8
55, 6, 7, 8
5, 6, 7, 8
...
file(3):
A, B, C, D
9, 10, 11, 12
9, 10, 11, 12
91, 10, 11, 12
...
exp ected输出(我们忽略了在上面的代码中的第一行的“A,BC,d”):
1+5+9, 2+6+10, ...
13+55+9, 2+6+10, ...
1+5+91, 2+6+10, ...
...
我希望找到在Java中有人高手谁可以教我如何让我的for循环要经过所有每行都有一个文件行,就像读取所有文件中的第一行并再次返回并读取所有文件中的第二行等等。
我会非常感谢你的帮助球员。
最佳
我建议你使用调试器来浏览你的代码,找出它在做什么。 –
您还应该关闭计算机并拿到一张纸和一支铅笔。用文字写出解决问题的步骤。 –
谢谢你们,我做了调试,这就是我发现它只读取第一行并停止。 –