import java.io.*;
public class MyExperiment {
public BufferedReader bufread;
String filepath, read;
double x[][] = new double[100][2];
int i, j;
public int readfilelen(String path) {
int f = 0;
filepath = path;
try {
File file = new File(filepath);
FileReader fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
while ((read = bufread.readLine()) != null)
f++;
bufread.close();
} catch (Exception e) {
}
return f;
}
public double[][] readfile(String path) {
int p = this.readfilelen(path);
x = new double[p][2];
try {
int a = 0;
i = j = 0;
String[] tempStr = new String[20];
filepath = path;
File file = new File(filepath);
FileReader fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
while ((read = bufread.readLine()) != null) {
tempStr = read.split("");
a = 0;
j = 0;
while (a < tempStr.length){
if (!tempStr[a].equals("")) {
x[i][j] = Double.parseDouble(tempStr[a]);
System.out.println("x[" + i + "][" + j + "]=" + x[i][j]);
j++;
}
a++;
}
i++;
}
} catch (Exception d) {}
return x;
}
public double computeMean(double[][] d, int seq) {
int i;
double meanvalue = 0;
for (i = 0; i < d.length; i++) {
meanvalue += d[i][seq];
}
meanvalue /= d.length;
return meanvalue;
}
public static void main(String args[]) { double numm[][];
double meanx, meany;
MyExperiment op = new MyExperiment();
numm = op.readfile("1.txt");
System.out.println("the number of (x,y) pair in the file is:"
+ numm.length);
meanx = op.computeMean(numm, 0);
meany = op.computeMean(numm, 1);
System.out.println("the mean number in x is:" + meanx);
System.out.println("the mean number in y is:" + meany);
}
}1.txt 的内容为
0.00  -6.44
0.10  12.55
0.20  3.22
0.30  -3.22
0.40  3.11
0.50  4.66
0.60  8.00
0.70  3.11
0.80  -3.44
0.90  -9.22
1.00  4.12
运行结果为:x[0][0]=0.0
the number of (x,y) pair in the file is:11
the mean number in x is:0.0
the mean number in y is:0.0为什么结果会这样,是哪里出问题呢