【问题标题】:PrintWriter not printing all data into my new text filePrintWriter 没有将所有数据打印到我的新文本文件中
【发布时间】:2021-03-30 16:03:45
【问题描述】:

我过去一天一直在处理一个项目,但我似乎无法让我的 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);
    }
}

【问题讨论】:

    标签: java eclipse file-io printwriter


    【解决方案1】:

    在我看来,您创建 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!
    }
    

    【讨论】:

    • 很有趣,谢谢。我想知道我是否可以使用 writer 作为这样的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多