如何将文件数据存储到多个对象中?

 收藏

class.txt

  • 雷纳·萨姆(Reena Sam),100、90、80、100,
  • 唐娜·D·巴托洛梅(Donna D. Bartolome),90、90、100、100,
  • 克里斯·崔(Chris Tui),100、90、100、70,

如何将文件数据行存储到多个对象中以计算成字母并在另一个文件中打印字母等级?

像这样:

  • 里纳·萨姆(Reena Sam):
  • 唐娜·D·巴托洛姆:B
  • 克里斯·崔(Chris Tui):C

以下是我目前在此方面的进展,但是我陷入了尝试存储数据的困境。请帮助...

...
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

class gradeMaker{
	private Scanner readFile;
    private String[] argument;
    private ArrayList<String> list; 
    private String finalGrade ="";
	
	gradeMaker(String [] arg) {
		argument = arg;
	}

	void readStudentScores() {
	
		File file = new File(argument[0]);
		if (!file.exists()) {
			System.out.println("Input file" + argument[0] + "does not exist");
			System.exit(2);
		}
	 
		try{  // Create a Scanner for the file
	    readFile = new Scanner(file);
	    ArrayList<Student> list = new ArrayList<>();
	  
        // Read data from a file
	    System.out.println("Read data from a file");
	    while (readFile.hasNextLine()) {
	    	String line = readFile.nextLine();   
	    	
	    	
	    	//list.add(new Student ()); //Stores Student student1 = new Student(Name2, 20, 28, 60,) 
	                                  //Stores Student student2 = new Student(Name2, 20, 28, 60,)      
	                                 
	    	
	    	System.out.println(line);
	    	
	    	//String[] studentInfo = line.split(",");
	    	
	    	
	   }
	    
	    Collections.sort(list);
	    for (int i = list.size(); i >= 0; i++)
	        System.out.print(list.get(i) + " ");
	    System.out.println(list.toString());
	    // System.out.println();
    	
	    
		}catch (FileNotFoundException e) {
			System.out.println("File: " + file + " not found");
		
		}//end catch
		catch (IOException e) {
			System.out.println("Error Reading from file:" + file + e.getMessage());
		
		}//end catch
		catch (Exception e) {
			System.out.println(e);
		
		}//end catch
		
		
		}
			

	void computeGrade() {
		
		int Q1 = quiz1;
		int Q2 = quiz2;
		int M1 = mid;
		int FN = finals;
			
		int finalScore = (Q1 * .20 + Q2 * .20 + M1 * .25 + FN * .35);
		if (finalScore >= .9) 
			finalGrade = "A";
		else if (finalScore >= .8 && finalScore <= .89 )
			finalGrade = "B";
		else if (finalScore >= .7 && finalScore <= .79 )
			finalGrade = "C";
		else if (finalScore >= .6 && finalScore <= .69 )
			finalGrade = "D";
		else if (finalScore <= .59 )
			finalGrade = "F";
		//}				
	}

	void Grades() {
		File outputFile = new File(argument[1]);
		try {
			PrintWriter output = new PrintWriter(outputFile);
			
			while (readFile.hasNextLine()) {
			output.println(finalGrade);
			}
		
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
		}		
	}

	
//Create student object
	class Student{
		private String name;
		private int quiz1;
		private int quiz2;
		private int  mid1;;
		private int finals;
		
		Student(){//default constructor
		}
		
		Student( int q1, int q2 ){
		quiz1 = q1; 
		quiz2 = q2; 
		quiz3 = q3; 
		quiz4 = q4; 
		}

		Student(int m1){
		mid1 = m1;
		mid2 = m2;
		}	

		Student(int f){
	   finals = f;
		}
		
		Student(int q1, int q2, int m1, int f){
		quiz1 = q1;
		quiz2 = q2;
		 mid1 = m1;
	        finals = f;
		}

		Student(String sName){
		this.name= sName;	
		}
		
		Student(String sName, int q1, int q2, int m1, int f){
		this.name= sName;
		quiz1 = q1;
		quiz2 = q2;
	        mid1 = m1;
		finals = f;
		}
		//get and set Student Name
		String getStudentName () {
			return name;
		}
		void setStudentName(String sName) {
			this.name = sName;
		}
		
		//get and set Quiz 1
		int getQuiz1Score () {
			return quiz1;
		}
		void setQuiz1Score(int q1) {
			this.quiz1 = q1;
		}
		
		//get and set Quiz 2
		int getQuiz2Score () {
			return quiz2;
		}
		void setQuiz2Score(int q2) {
			this.quiz2 = q2;
		}

		//get and set Midterm 1
		int getMid1Score () {
			return mid1;
		}
		void setMid1Score(int m1) {
			this.mid1 = m1;
		}
		
		//get and set Final exam
		int getFinalScore () {
			return finals;
		}
		void setFinalScore(int f) {
			this.finals = f;
		}
	}
                   
}
回复
  • yeum 回复

    这里有几件事。首先,我看到您有一个系统打印集,可以在您的收藏夹排序中捕获未知错误。 printStackTrace在那里将更加有用。

    另外,您的PrintWriter仅返回成绩。为了获得所需的输出,您确实要返回带有串联字符串的构造函数。您可以通过创建一个类的对象来处理需要返回的参数来做到这一点。就像是:

    String finalGrade = f.toString();
    
    public gradeMarker(String name, String finalGrade) {
         this.name = name;
         this.finalGrade = finalGrade;
     } 
    

    您的最终成绩为整数(f),但我个人认为以字符串形式读取/写入数据会更容易。您可以使用getter和setter方法进行后续操作,然后放置一个最终方法以返回所需的输出。就像是

    public String toString() {
        return name + "," + finalGrade;
    }
    

    然后,您可以将结果放入列表中,并使用for每个循环将其写入文件

    List<gradeMaker> grade = FileReader(//your file)
    PrintWriter output = new PrintWriter(//your file)
    for (gradeMaker x : grade) {
        output.println(x);
    }
    

    这将打印每一行,并且因为只有每一行才存在要打印的对象。希望所有这些对您有所帮助!