我已经解决了这个问题好几天了,无法让它发挥作用。问题要求我打开一个文件并计算平均值,然后关闭它。然后重新打开它计算标准偏差。我遇到的麻烦是在我计算平均值并关闭文件之后。当我尝试重新打开它时显示无法找到该文件的错误。以下是我到目前为止的情况:
File fr = new File(filename);
Scanner inputFile = new Scanner(fr);
filename = inputFile.nextLine();
while(inputFile.hasNext())
{
double number = inputFile.nextDouble();
sum = number + sum;
count++;
line = inputFile.nextDouble();
}
inputFile.close();
mean = sum / count;
//New File
File file = new File(filename);
Scanner br = new Scanner(file);
double sumB = 0;
int countB = 0;
filename = br.nextLine();
while(br.hasNext())
{
double sumthin = br.nextDouble();
difference = sumthin - mean;
sumB = sumB + Math.sqrt(difference);
count++;
line = br.nextDouble();
}
br.close();
Math.sqrt(sumB / count);
答案 0 :(得分:4)
你(可能)没有两次打开同一个文件 - 你正在读取第一个文件中的文件名,然后打开第二个文件:
File fr = new File(filename);
...
filename = inputFile.nextLine();
...
File file = new File(filename);