在函数中使用 FileWriter;文件应该已经存在
Using FileWriter Within a Function; File Should Already Exist
好的,所以在 distributionA 函数中,我想打开并写入我已经在 main 中成功写入的同一个文件,但它不会将 writer 识别为变量。我尝试使用追加,但它有时会覆盖我想保留的文本。我很茫然,我怎样才能有效地让这段代码做我需要的事情呢?即写入文件"outputScores.txt"。
澄清一下:我不想使用 System.out。我只是把它留在那里,因为我在使用 FileWriter 时遇到错误。
import java.io.*;
import java.util.*;
public class readFile {
/* Distribution function: Steps are:
* -assign an array of doubles for each grade.
* -pass each array into getAverage, getMedian, and getStandardDeviation to get each individual statistic
* -print out each line within the function.
*/
public static void distributionA(double[]score,int size) throws IOException{
int countA=0,countAp=0,countAm= 0,countBp=0, countBm=0,countB=0,countCp =0, countCm=0,countC=0,countD=0,countDp=0,countDm=0,countF=0;
double averageA,averageAp,averageAm,averageB,averageBp,averageBm,averageC,averageCp,averageCm,averageDp,averageDm,averageD,averageF;
double medianA,medianAp,medianAm,medianB,medianBp,medianBm,medianCp,medianCm,medianC,medianDp,medianDm,medianD,medianF;
double deviationA,deviationAp,deviationAm,deviationBp,deviationBm,deviationB,deviationCp,deviationCm,deviationC,deviationDp,deviationDm,deviationD,deviationF;
double []arrayA = new double[100];
double []arrayAp = new double[100];
double []arrayAm = new double[100];
double []arrayB = new double[100];
double []arrayBp = new double[100];
double []arrayBm = new double[100];
double []arrayC= new double[100];
double []arrayCp= new double[100];
double []arrayCm= new double[100];
double []arrayD= new double[100];
double []arrayDp= new double[100];
double []arrayDm= new double[100];
double []arrayF= new double[100];
for(int i=0; i<size-1;i++){
if(score[i] >=97){arrayAp[countAp] = score[i];countAp++;}
else if(score[i] >= 93 && score[i] <97){arrayA[countA] = score[i];countA++;}
else if(score[i] >=90 && score[i] <94){arrayAm[countAm] = score[i];countAm++;}
else if(score[i] >=87 && score[i] <90){arrayBp[countBp] = score[i];countBp++;}
else if(score[i] >= 83 && score[i] <87){arrayB[countB] = score[i];countB++;}
else if(score[i] >= 80 && score[i] <83){arrayBm[countBm] = score[i];countBm++;}
else if (score[i] >=73 && score[i] <77){arrayC[countC] = score[i];countC++;}
else if (score[i] >=70 && score[i] <73){arrayCm[countCm] = score[i];countCm++;}
else if (score[i] >=77 && score[i] <80){arrayCp[countCp] = score[i];countCp++;}
else if(score[i] >=63 && score[i] < 67){arrayD[countD] = score[i];countD++;}
else if(score[i] >=67 && score[i] < 70){arrayDp[countDp] = score[i];countDp++;}
else if(score[i] >=60 && score[i] < 63){arrayDm[countDm] = score[i];countDm++;}
else if (score[i] < 60){arrayF[countF] = score[i];countF++;}
}
averageA = getAverage(arrayA,countA);
averageAp= getAverage(arrayAp,countAp);
averageAm=getAverage(arrayAm,countAm);
averageB= getAverage(arrayB,countB);
averageBp=getAverage(arrayBp,countBp);
averageBm=getAverage(arrayBm,countBm);
averageC=getAverage(arrayC,countC);
averageCp=getAverage(arrayCp,countCp);
averageCm=getAverage(arrayCm,countCm);
averageD=getAverage(arrayD,countD);
averageDp=getAverage(arrayDp,countDp);
averageDm=getAverage(arrayDm,countDm);
averageF= getAverage(arrayF,countF);
medianAp= getMedian(arrayAp,countAp);
medianAm= getMedian(arrayAm,countAm);
medianA= getMedian(arrayA,countA);
medianB= getMedian(arrayB,countB);
medianBp= getMedian(arrayBp,countBp);
medianBm= getMedian(arrayBm,countBm);
medianC=getMedian(arrayC,countC);
medianCp=getMedian(arrayCp,countCp);
medianCm=getMedian(arrayCm,countCm);
medianD=getMedian(arrayD, countD);
medianDp=getMedian(arrayDp, countDp);
medianDm=getMedian(arrayDm, countDm);
medianF=getMedian(arrayF,countF);
deviationA=getStandardDeviation(arrayA,countA);
deviationAp=getStandardDeviation(arrayAp,countAp);
deviationAm=getStandardDeviation(arrayAm,countAm);
deviationB=getStandardDeviation(arrayB,countB);
deviationBp=getStandardDeviation(arrayBp,countBp);
deviationBm=getStandardDeviation(arrayBm,countBm);
deviationC=getStandardDeviation(arrayC,countC);
deviationCp=getStandardDeviation(arrayCp,countCp);
deviationCm=getStandardDeviation(arrayCm,countCm);
deviationD=getStandardDeviation(arrayD,countD);
deviationDp=getStandardDeviation(arrayDp,countDp);
deviationDm=getStandardDeviation(arrayDm,countDm);
deviationF=getStandardDeviation(arrayF,countF);
System.out.println("A+: Occurances" + countAp + ", Average: " + averageAp + ", Median: " + medianAp + ", Standard Deviation: " +deviationAp);
System.out.println("A: Occurances" + countA + ", Average: " + averageA + ", Median: " + medianA + ", Standard Deviation: " +deviationA);
System.out.println("A-: Occurances" + countAm + ", Average: " + averageAm + ", Median: " + medianAm + ", Standard Deviation: " +deviationAm);
System.out.println("B+: Occurances" + countBp + ", Average: " + averageBp + ", Median: " + medianBp + ", Standard Deviation: " +deviationBp);
System.out.println("B: Occurances" + countB + ", Average: " + averageB + ", Median: " + medianB + ", Standard Deviation: " +deviationB);
System.out.println("B-: Occurances" + countBm + ", Average: " + averageBm + ", Median: " + medianBm + ", Standard Deviation: " +deviationBm);
System.out.println("C+: Occurances" + countCp + ", Average: " + averageCp + ", Median: " + medianCp + ", Standard Deviation: " +deviationCp);
System.out.println("C: Occurances" + countC + ", Average: " + averageC + ", Median: " + medianC + ", Standard Deviation: " +deviationC);
System.out.println("C-: Occurances" + countCm + ", Average: " + averageCm + ", Median: " + medianCm + ", Standard Deviation: " +deviationCm);
System.out.println("D+: Occurances" + countDp + ", Average: " + averageDp + ", Median: " + medianDp + ", Standard Deviation: " +deviationDp);
System.out.println("D: Occurances" + countD + ", Average: " + averageD + ", Median: " + medianD + ", Standard Deviation: " +deviationD);
System.out.println("D-: Occurances" + countDm + ", Average: " + averageDm + ", Median: " + medianDm + ", Standard Deviation: " +deviationDm);
System.out.println("F: Occurances" + countF + ", Average: " + averageF + ", Median: " + medianF + ", Standard Deviation: " +deviationF);
}
/* function getAverage steps:
* -run through an array, adding the value at each location to the total.
* dividing that total by the size of the array.
*/
public static double getAverage(double [] score, int size){
double total = 0;
double average = 0;
for (int i = 0; i< size; i++){
total += score[i];
}
if (size!=0){
average = total/(size);
return average;}
/*to fix the case where we divide by zero, I did the following:*/
else{average = 0; return average;}/*There is only one case where the size will be zero... when there is nothing in the array*/
}
/*function getMedian steps:
* -sort the array in ascending order using a simple bubble sort.
* -take the middle term; this is the median
*/
public static double getMedian(double [] score, int size){
double temp; boolean swapOccurred;
do {swapOccurred = false;
for (int i = 0; i<size;i++)
{if (score[i]< 0){score[i] = score[i] *-1;}
if (score[i] >score[i+1])
{
temp = score[i];
score[i] = score[i+1];
score[i+1] = temp;
swapOccurred = true;
}
}
}while(swapOccurred);
return score[(size)/2];
}
/*function getStandardDeviation steps:
* -get the average from the array using the getAverage function.
* -run through the array, and subtract the average from each term.
* -square that number, and add it to the sum total.
* -finally, divide that variable by (n-1).
*/
public static double getStandardDeviation(double []score, int size){
double average = getAverage(score,size);
double sum;double sumtotal=0;double hold;double step;double deviation;
for (int i = 0; i<size;i++){
sum = score[i]-average;
hold = sum*sum;
sumtotal += hold;
}
step = sumtotal/(size-1);
/*special case handling. We can't divide by zero, so in the case that there is only one element, we just return the deviation as zero.*/
if (size!=1){
deviation = Math.sqrt(step);
return deviation;}
else{deviation = 0;return deviation;}
}
public static void main(String[] args)/*main implementation; open the scanner, and read each element of the file.*/
throws IOException{
PrintWriter writer = new PrintWriter("outputScores.txt");
int size = 0;
double [] scores = new double[100];
Scanner input = new Scanner(new File("scores.dat"));
while(input.hasNext()){
if (input.hasNextDouble()){
double store = input.nextDouble();/*To make sure that the numbers are only within our accepted range*/
if(store > 0 && store <= 100){
scores[size]=store;
size++;/*Increase the current size of the array*/
}
}
else{
input.next();
}
}
double deviation = getStandardDeviation(scores,size);
double average = getAverage(scores, size);
double median = getMedian(scores,size);
writer.println("1. Enrolled student number: " + (size));
writer.println("2. Statistics for all students");
writer.println(" - Average: " + average);
writer.println(" - Median: " + median);
writer.println(" - Standard Deviation: " + deviation);
distributionA(scores,size);
writer.close();
}
}
您需要将 writer
传递给 distributionA()
以便它可以使用它:
distributionA(writer, scores, size);
您必须更改方法签名:
public static void distributionA(PrintWriter writer, double[]score, int size)
然后,在 distributionA 里面,你可以写入:
writer.println("...");
好的,所以在 distributionA 函数中,我想打开并写入我已经在 main 中成功写入的同一个文件,但它不会将 writer 识别为变量。我尝试使用追加,但它有时会覆盖我想保留的文本。我很茫然,我怎样才能有效地让这段代码做我需要的事情呢?即写入文件"outputScores.txt"。 澄清一下:我不想使用 System.out。我只是把它留在那里,因为我在使用 FileWriter 时遇到错误。
import java.io.*;
import java.util.*;
public class readFile {
/* Distribution function: Steps are:
* -assign an array of doubles for each grade.
* -pass each array into getAverage, getMedian, and getStandardDeviation to get each individual statistic
* -print out each line within the function.
*/
public static void distributionA(double[]score,int size) throws IOException{
int countA=0,countAp=0,countAm= 0,countBp=0, countBm=0,countB=0,countCp =0, countCm=0,countC=0,countD=0,countDp=0,countDm=0,countF=0;
double averageA,averageAp,averageAm,averageB,averageBp,averageBm,averageC,averageCp,averageCm,averageDp,averageDm,averageD,averageF;
double medianA,medianAp,medianAm,medianB,medianBp,medianBm,medianCp,medianCm,medianC,medianDp,medianDm,medianD,medianF;
double deviationA,deviationAp,deviationAm,deviationBp,deviationBm,deviationB,deviationCp,deviationCm,deviationC,deviationDp,deviationDm,deviationD,deviationF;
double []arrayA = new double[100];
double []arrayAp = new double[100];
double []arrayAm = new double[100];
double []arrayB = new double[100];
double []arrayBp = new double[100];
double []arrayBm = new double[100];
double []arrayC= new double[100];
double []arrayCp= new double[100];
double []arrayCm= new double[100];
double []arrayD= new double[100];
double []arrayDp= new double[100];
double []arrayDm= new double[100];
double []arrayF= new double[100];
for(int i=0; i<size-1;i++){
if(score[i] >=97){arrayAp[countAp] = score[i];countAp++;}
else if(score[i] >= 93 && score[i] <97){arrayA[countA] = score[i];countA++;}
else if(score[i] >=90 && score[i] <94){arrayAm[countAm] = score[i];countAm++;}
else if(score[i] >=87 && score[i] <90){arrayBp[countBp] = score[i];countBp++;}
else if(score[i] >= 83 && score[i] <87){arrayB[countB] = score[i];countB++;}
else if(score[i] >= 80 && score[i] <83){arrayBm[countBm] = score[i];countBm++;}
else if (score[i] >=73 && score[i] <77){arrayC[countC] = score[i];countC++;}
else if (score[i] >=70 && score[i] <73){arrayCm[countCm] = score[i];countCm++;}
else if (score[i] >=77 && score[i] <80){arrayCp[countCp] = score[i];countCp++;}
else if(score[i] >=63 && score[i] < 67){arrayD[countD] = score[i];countD++;}
else if(score[i] >=67 && score[i] < 70){arrayDp[countDp] = score[i];countDp++;}
else if(score[i] >=60 && score[i] < 63){arrayDm[countDm] = score[i];countDm++;}
else if (score[i] < 60){arrayF[countF] = score[i];countF++;}
}
averageA = getAverage(arrayA,countA);
averageAp= getAverage(arrayAp,countAp);
averageAm=getAverage(arrayAm,countAm);
averageB= getAverage(arrayB,countB);
averageBp=getAverage(arrayBp,countBp);
averageBm=getAverage(arrayBm,countBm);
averageC=getAverage(arrayC,countC);
averageCp=getAverage(arrayCp,countCp);
averageCm=getAverage(arrayCm,countCm);
averageD=getAverage(arrayD,countD);
averageDp=getAverage(arrayDp,countDp);
averageDm=getAverage(arrayDm,countDm);
averageF= getAverage(arrayF,countF);
medianAp= getMedian(arrayAp,countAp);
medianAm= getMedian(arrayAm,countAm);
medianA= getMedian(arrayA,countA);
medianB= getMedian(arrayB,countB);
medianBp= getMedian(arrayBp,countBp);
medianBm= getMedian(arrayBm,countBm);
medianC=getMedian(arrayC,countC);
medianCp=getMedian(arrayCp,countCp);
medianCm=getMedian(arrayCm,countCm);
medianD=getMedian(arrayD, countD);
medianDp=getMedian(arrayDp, countDp);
medianDm=getMedian(arrayDm, countDm);
medianF=getMedian(arrayF,countF);
deviationA=getStandardDeviation(arrayA,countA);
deviationAp=getStandardDeviation(arrayAp,countAp);
deviationAm=getStandardDeviation(arrayAm,countAm);
deviationB=getStandardDeviation(arrayB,countB);
deviationBp=getStandardDeviation(arrayBp,countBp);
deviationBm=getStandardDeviation(arrayBm,countBm);
deviationC=getStandardDeviation(arrayC,countC);
deviationCp=getStandardDeviation(arrayCp,countCp);
deviationCm=getStandardDeviation(arrayCm,countCm);
deviationD=getStandardDeviation(arrayD,countD);
deviationDp=getStandardDeviation(arrayDp,countDp);
deviationDm=getStandardDeviation(arrayDm,countDm);
deviationF=getStandardDeviation(arrayF,countF);
System.out.println("A+: Occurances" + countAp + ", Average: " + averageAp + ", Median: " + medianAp + ", Standard Deviation: " +deviationAp);
System.out.println("A: Occurances" + countA + ", Average: " + averageA + ", Median: " + medianA + ", Standard Deviation: " +deviationA);
System.out.println("A-: Occurances" + countAm + ", Average: " + averageAm + ", Median: " + medianAm + ", Standard Deviation: " +deviationAm);
System.out.println("B+: Occurances" + countBp + ", Average: " + averageBp + ", Median: " + medianBp + ", Standard Deviation: " +deviationBp);
System.out.println("B: Occurances" + countB + ", Average: " + averageB + ", Median: " + medianB + ", Standard Deviation: " +deviationB);
System.out.println("B-: Occurances" + countBm + ", Average: " + averageBm + ", Median: " + medianBm + ", Standard Deviation: " +deviationBm);
System.out.println("C+: Occurances" + countCp + ", Average: " + averageCp + ", Median: " + medianCp + ", Standard Deviation: " +deviationCp);
System.out.println("C: Occurances" + countC + ", Average: " + averageC + ", Median: " + medianC + ", Standard Deviation: " +deviationC);
System.out.println("C-: Occurances" + countCm + ", Average: " + averageCm + ", Median: " + medianCm + ", Standard Deviation: " +deviationCm);
System.out.println("D+: Occurances" + countDp + ", Average: " + averageDp + ", Median: " + medianDp + ", Standard Deviation: " +deviationDp);
System.out.println("D: Occurances" + countD + ", Average: " + averageD + ", Median: " + medianD + ", Standard Deviation: " +deviationD);
System.out.println("D-: Occurances" + countDm + ", Average: " + averageDm + ", Median: " + medianDm + ", Standard Deviation: " +deviationDm);
System.out.println("F: Occurances" + countF + ", Average: " + averageF + ", Median: " + medianF + ", Standard Deviation: " +deviationF);
}
/* function getAverage steps:
* -run through an array, adding the value at each location to the total.
* dividing that total by the size of the array.
*/
public static double getAverage(double [] score, int size){
double total = 0;
double average = 0;
for (int i = 0; i< size; i++){
total += score[i];
}
if (size!=0){
average = total/(size);
return average;}
/*to fix the case where we divide by zero, I did the following:*/
else{average = 0; return average;}/*There is only one case where the size will be zero... when there is nothing in the array*/
}
/*function getMedian steps:
* -sort the array in ascending order using a simple bubble sort.
* -take the middle term; this is the median
*/
public static double getMedian(double [] score, int size){
double temp; boolean swapOccurred;
do {swapOccurred = false;
for (int i = 0; i<size;i++)
{if (score[i]< 0){score[i] = score[i] *-1;}
if (score[i] >score[i+1])
{
temp = score[i];
score[i] = score[i+1];
score[i+1] = temp;
swapOccurred = true;
}
}
}while(swapOccurred);
return score[(size)/2];
}
/*function getStandardDeviation steps:
* -get the average from the array using the getAverage function.
* -run through the array, and subtract the average from each term.
* -square that number, and add it to the sum total.
* -finally, divide that variable by (n-1).
*/
public static double getStandardDeviation(double []score, int size){
double average = getAverage(score,size);
double sum;double sumtotal=0;double hold;double step;double deviation;
for (int i = 0; i<size;i++){
sum = score[i]-average;
hold = sum*sum;
sumtotal += hold;
}
step = sumtotal/(size-1);
/*special case handling. We can't divide by zero, so in the case that there is only one element, we just return the deviation as zero.*/
if (size!=1){
deviation = Math.sqrt(step);
return deviation;}
else{deviation = 0;return deviation;}
}
public static void main(String[] args)/*main implementation; open the scanner, and read each element of the file.*/
throws IOException{
PrintWriter writer = new PrintWriter("outputScores.txt");
int size = 0;
double [] scores = new double[100];
Scanner input = new Scanner(new File("scores.dat"));
while(input.hasNext()){
if (input.hasNextDouble()){
double store = input.nextDouble();/*To make sure that the numbers are only within our accepted range*/
if(store > 0 && store <= 100){
scores[size]=store;
size++;/*Increase the current size of the array*/
}
}
else{
input.next();
}
}
double deviation = getStandardDeviation(scores,size);
double average = getAverage(scores, size);
double median = getMedian(scores,size);
writer.println("1. Enrolled student number: " + (size));
writer.println("2. Statistics for all students");
writer.println(" - Average: " + average);
writer.println(" - Median: " + median);
writer.println(" - Standard Deviation: " + deviation);
distributionA(scores,size);
writer.close();
}
}
您需要将 writer
传递给 distributionA()
以便它可以使用它:
distributionA(writer, scores, size);
您必须更改方法签名:
public static void distributionA(PrintWriter writer, double[]score, int size)
然后,在 distributionA 里面,你可以写入:
writer.println("...");