简体   繁体   English

PrintWriter 没有将所有数据打印到我的新文本文件中

[英]PrintWriter not printing all data into my new text file

I have a project I have been working on for the past day and I can't seem to get my PrintWriter to write all of my data into the new text file.我有一个过去一天一直在做的项目,我似乎无法让我的 PrintWriter 将我的所有数据写入新的文本文件。

Right now I have pretty much everything done, the correct info prints to the console but when I open the new text file I see only the names and no final grade or letter grade.现在我几乎完成了所有工作,正确的信息打印到控制台,但是当我打开新的文本文件时,我只看到名称,没有最终成绩或字母成绩。 What am I doing wrong here?我在这里做错了什么?

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);
    }
}

It seems to me that you are creating the PrintWriter too much.在我看来,您创建 PrintWriter 太多了。

Each time you create a new PrintWriter, you are overwriting the file that was already there.每次创建新的 PrintWriter 时,都会覆盖已经存在的文件。 As well, the main printwriter gets closed after the other ones (eg., in calcWeightedAverage) have been opened and closed.同样,在打开和关闭其他打印机(例如,在 calcWeightedAverage 中)之后,主打印机将关闭。

Instead, I would recommend only opening one printwriter, and passing it to the other functions.相反,我建议只打开一台打印机,然后将其传递给其他函数。

For example,例如,

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM