简体   繁体   中英

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.

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.

Each time you create a new PrintWriter, you are overwriting the file that was already there. As well, the main printwriter gets closed after the other ones (eg., in calcWeightedAverage) have been opened and closed.

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!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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