[英]PrintWriter not printing all data into my new text file
我有一个过去一天一直在做的项目,我似乎无法让我的 PrintWriter 将我的所有数据写入新的文本文件。
现在我几乎完成了所有工作,正确的信息打印到控制台,但是当我打开新的文本文件时,我只看到名称,没有最终成绩或字母成绩。 我在这里做错了什么?
import java.io.*;
import java.util.Scanner;
public class StudentGrades {
public static void main(String[] args) throws IOException {
File grades = new File("M:\\java stuff\\grades.txt");
FileWriter pencil = new FileWriter("M:\\java stuff\\output.txt");
PrintWriter writer = new PrintWriter(pencil);
Scanner scan = new Scanner(grades);
double[] array = new double [6];
try {
while(scan.hasNext()) {
String firstName = scan.next();
String lastName = scan.next();
double g1 = scan.nextInt();
array[0] = g1;
double g2 = scan.nextInt();
array[1] = g2;
double g3 = scan.nextInt();
array[2] = g3;
double g4 = scan.nextInt();
array[3] = g4;
double g5 = scan.nextInt();
array[4] = g5;
double g6 = scan.nextInt();
array[5] = g6;
System.out.printf(firstName + " ");
System.out.printf(lastName + ", ");
writer.print(firstName + " ");
writer.print(lastName + ",");
StudentGrades.getLetterGrade(StudentGrades.calcWeightedAvg(g1, g2, g3, g4, g5, g6));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (pencil != null) {
pencil.close();
scan.close();
writer.close();//
}
}
}
public static double calcWeightedAvg(double g1, double g2, double g3, double g4, double g5, double g6) throws IOException {
FileWriter pencil = new FileWriter("M:\\java stuff\\output.txt");
PrintWriter writer = new PrintWriter(pencil);
double[] weights = {10.5, 10.5, 18, 18, 18, 25};
g1 = g1 * weights[0];
g2 = g2 * weights[1];
g3 = g3 * weights[2];
g4 = g4 * weights[3];
g5 = g5 * weights[4];
g6 = g6 * weights[5];
double sum = g1 + g2 + g3 + g4 + g5 + g6;
double avg = sum/100;
writer.write(avg + " ");
System.out.print(avg +", ");
pencil.close();
writer.close();
return avg;
}
public static void getLetterGrade(double avg) throws IOException {
FileWriter pencil = new FileWriter("M:\\java stuff\\output.txt");
PrintWriter writer = new PrintWriter(pencil);
char LetterGrade;
int i = 1;
do {
if(avg>=90.0) {
LetterGrade = 'A';
System.out.print(LetterGrade + "\n");
writer.write(LetterGrade + "\n");
break;
}
if(avg>=80.0) {
LetterGrade = 'B';
System.out.print(LetterGrade + "\n");
writer.write(LetterGrade + "\n");
break;
}
if(avg>=70.0) {
LetterGrade = 'C';
System.out.print(LetterGrade + "\n");
writer.write(LetterGrade + "\n");
break;
}
if(avg>=60.0) {
LetterGrade = 'D';
System.out.print(LetterGrade + "\n");
writer.write(LetterGrade + "\n");
break;
}
else {
LetterGrade = 'F';
System.out.print(LetterGrade + "\n");
writer.write(LetterGrade + "\n");
}i++;
pencil.close();
writer.close();
} while(i<2);
}
}
在我看来,您创建 PrintWriter 太多了。
每次创建新的 PrintWriter 时,都会覆盖已经存在的文件。 同样,在打开和关闭其他打印机(例如,在 calcWeightedAverage 中)之后,主打印机将关闭。
相反,我建议只打开一台打印机,然后将其传递给其他函数。
例如,
public static double calcWeightedAvg(PrintWriter writer, double g1, double g2, double g3, double g4, double g5, double g6) throws IOException {
writer.write("blah");
// do NOT close writer here!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.