java 重复输出的一部分

java repeat one part of the output

我有Java作业,这是对 在您的程序中创建三个 classes:InputHandlerOutputHandler 和 Student classes。 InputHandler class 处理从输入文件中读取信息。它还包含一个 Student 对象数组,用于在其中存储信息。 OutputHandler class 获取通过 InputHandler class 中的方法提供的学生数组。然后,它应该在“results.txt”文件中打印每个学生的全部记录以及计算出的平均值。它还应该计算并打印标准偏差。最后,Student class,包含存储学生信息的字段。它还有一个名为 getQuizAverage() 的方法,该方法计算并 returns 该特定学生的测验平均值,OutputHandler 使用该方法将其打印在结果文件中。

输入文件

input.txt:

99091;穆罕默德;7;5.5;7;8;9

99092;哈立德;7;5.5;7;8;8

99093;阿里;7;5.5;7;8;9

99094;艾哈迈德;7;5.5;7;8;9

Output.txt

99091 穆罕默德 7 5.5 7 8 9 7.3

99092 哈立德 7 5.5 7 8 8 7.1

99093 阿里 7 5.5 7 8 9 7.3

99094 艾哈迈德 7 5.5 7 8 9 7.3

我只得到一个重复输出的问题

99094 艾哈迈德 7 5.5 7 8 9 7.3

99094 艾哈迈德 7 5.5 7 8 9 7.3

99094 艾哈迈德 7 5.5 7 8 9 7.3

99094 艾哈迈德 7 5.5 7 8 9 7.3

我的 classes 我有 4 classes , Student , inputhandler , outputhandler , main

public class Student {

    public String Name;
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getID() {
        return ID;
    }

    public void setID(String iD) {
        ID = iD;
    }
    public double getQuiz1() {
        return quiz1;
    }

    public void setQuiz1(double quiz1) {
        this.quiz1 = quiz1;
    }
    public double getQuiz2() {
        return quiz2;
    }
    public void setQuiz2(double quiz2) {
        this.quiz2 = quiz2;
    }

    public double getQuiz3() {
        return quiz3;
    }

    public void setQuiz3(double quiz3) {
        this.quiz3 = quiz3;
    }

    public double getQuiz4() {
        return quiz4;
    }

    public void setQuiz4(double quiz4) {
        this.quiz4 = quiz4;
    }

    public double getQuiz5() {
        return quiz5;
    }

    public void setQuiz5(double quiz5) {
        this.quiz5 = quiz5;
    }

    public double getAvgQuiz() {
        return avgQuiz;
    }

    public void setAvgQuiz(double avgQuiz) {
        this.avgQuiz = avgQuiz;
    }

    public  String ID;
    public double quiz1;
    public double quiz2;
    public double quiz3;
    public double quiz4;
    public double quiz5;

    public double avgQuiz;

    public double getQuizAverage()
    {
       return ((quiz1+quiz2+quiz3+quiz4+quiz5)/5);
    }

    public Student() {
        // TODO Auto-generated constructor stub
    }

}

输入处理程序

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class InputHandler {

    Student[] myStudent = new Student[4];

    public InputHandler(String fileName) {
        this.inputData(fileName);
    }

    private void inputData(String fileName) {

        try {
            File file = new File(fileName);
            FileReader fileReader = new FileReader(file);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String line;


            while ((line = bufferedReader.readLine())!=null)
            {
            String[] parts = line.split(";");


                for(int i=0;i<4;i++)
                {

                    myStudent[i] = new Student();
                    myStudent[i].setID(parts[0]);
                    myStudent[i].setName(parts[1]);
                    myStudent[i].setQuiz1(Double.parseDouble(parts[2]));
                    myStudent[i].setQuiz2(Double.parseDouble(parts[3]));
                    myStudent[i].setQuiz3(Double.parseDouble(parts[4]));
                    myStudent[i].setQuiz4(Double.parseDouble(parts[5]));
                    myStudent[i].setQuiz5(Double.parseDouble(parts[6]));
                }     

        }


            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public Student [] getStudents() {
        return myStudent;
    }
}

输出处理器

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class OutputHandler {

    Student[] myStudent;

    public OutputHandler(Student[] stds) {

        myStudent = stds;
    }

    void writeData() throws IOException {
        FileWriter writt = new FileWriter("output.txt");

        for (int i = 0; i < 4; i++) {

            writt.write(myStudent[i].ID + '\t' + myStudent[i].Name + '\t'
                    + '\t' + myStudent[i].quiz1 + '\t' + myStudent[i].quiz2
                    + '\t' + myStudent[i].quiz3 + '\t' + myStudent[i].quiz4
                    + '\t' + myStudent[i].quiz5 + '\t'+"  "+myStudent[i].getQuizAverage());
            writt.write('\n');

        }

        writt.close();

    }
}

主要功能

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {



    public static void main(String[] args) throws IOException {
        InputHandler ih=new InputHandler("input.txt");
        OutputHandler output1 = new OutputHandler(ih.getStudents());
        output1.writeData();    

    }

}

您应该删除 InputHandler class inputData 方法中的 for 循环。因为你一行一行的看学生信息。再次循环是错误的。应该是这样

private void inputData(String fileName) {

    try {
        File file = new File(fileName);
        FileReader fileReader = new FileReader(file);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line;

        int studentIndex = 0;
        while ((line = bufferedReader.readLine())!=null)
        {
           String[] parts = line.split(";");
           myStudent[studentIndex] = new Student();
           myStudent[studentIndex].setID(parts[0]);
           myStudent[studentIndex].setName(parts[1]);
           myStudent[studentIndex].setQuiz1(Double.parseDouble(parts[2]));
           myStudent[studentIndex].setQuiz2(Double.parseDouble(parts[3]));
           myStudent[studentIndex].setQuiz3(Double.parseDouble(parts[4]));
           myStudent[studentIndex].setQuiz4(Double.parseDouble(parts[5]));
           myStudent[studentIndex].setQuiz5(Double.parseDouble(parts[6]));
           studentIndex++;

    }
        fileReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

在输入 header 中,您正试图 link 同一行的部分到所有 objects。也需要将行放入for循环中。