当前位置:   article > 正文

Java输入输出流练习:从csv文件读取学生成绩,计算综合成绩并输出到文件_从“students.grades.csv”文件中读取学生成绩,处理好缺失值,并输出;

从“students.grades.csv”文件中读取学生成绩,处理好缺失值,并输出;

Java 流练习:从csv文件读取学生成绩,计算综合成绩并输出到文件


  • 1

package project005;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

class Student
{
private String id;
private String name;
private float PingShiScore;
private float ShiYanScore;
private float QiMoScore;
private Integer ZongHeScore;

public Student() {
	super();
}

public Student(String id, String name, float PingShiScore, float ShiYanScore, float QiMoScore, int ZongHeScore)
{
	super();
	this.id = id;
	this.name = name;
	this.PingShiScore = PingShiScore;
	this.ShiYanScore = ShiYanScore;
	this.QiMoScore = QiMoScore;
	this.ZongHeScore = ZongHeScore;
}

public String getId() {
	return id;
}

public void setId(String id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public float getPingShiScore() {
	return PingShiScore;
}

public void setPingShiScore(float PingShiScore) {
	this.PingShiScore = PingShiScore;
}

public float getShiYanScore() {
	return ShiYanScore;
}

public void setShiYanScore(float ShiYanScore) {
	this.ShiYanScore = ShiYanScore;
}

public float getQiMoScore() {
	return QiMoScore;
}

public void setQiMoScore(float QiMoScore) {
	this.QiMoScore = QiMoScore;
}

public int getZongHeScore() {
	return ZongHeScore;
}

public void setZongHeScore(int ZongHeScore) {
	this.ZongHeScore = ZongHeScore;
}

@Override
public String toString() {
	return "Student [id=" + id 
			+ ", name=" + name
			+ ", PingShiScore=" + PingShiScore
			+ ", ShiYanScore=" + ShiYanScore
			+ ", QiMoScore=" + QiMoScore
			+ ", ZongHeScore=" + ZongHeScore + "]";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72

}

public class CalculateAndArrange
{
public static void main(String[] args)
{

	File fname = new File("某课程成绩表.csv");
	InputStreamReader fr = null;  
	BufferedReader br = null;  
	try {  
		fr = new InputStreamReader(new FileInputStream(fname));  
		br = new BufferedReader(fr);  
		String rec = null;  
		String[] argsArr = null;  
		
		
		
		String [][]s = new String[42][6];
		int count = 0;
		while ((rec = br.readLine()) != null) {  
			argsArr = rec.split(","); 
			for (int i = 0; i < argsArr.length; i++)
			{
				s[count][i] = argsArr[i];
			}
			count++;
		}
		
		Student[] stu = new Student[50];
		for(int i = 1; i < count; i++)
		{
			stu[i] = new Student();
            stu[i].setId(s[i][0]);
            stu[i].setName(s[i][1]);
            stu[i].setPingShiScore(Float.parseFloat(s[i][2]));
            stu[i].setShiYanScore(Float.parseFloat(s[i][3]));
            stu[i].setQiMoScore(Float.parseFloat(s[i][4]));
            stu[i].setZongHeScore((int) Math.round(stu[i].getPingShiScore() * 0.2 + stu[i].getShiYanScore() * 0.3 + stu[i].getQiMoScore() * 0.5)); 
            
		}
		
		
		
		for(int i = 1; i < count; i++)
		{
			System.out.println(stu[i]);
		}
		
		
		for(int i = 1; i < count; i++)
		{
			File f=new File(stu[i].getId() + stu[i].getName() + ".txt");
			FileOutputStream fos = new FileOutputStream(f);
			OutputStreamWriter dos = new OutputStreamWriter(fos);
			dos.write("学号:" + stu[i].getId() + "\r\n");
			dos.write("姓名:" + stu[i].getName() + "\r\n");
			dos.write("平时成绩:" + (int) stu[i].getPingShiScore() + "\r\n");
			dos.write("实验成绩:" + (int) stu[i].getShiYanScore() + "\r\n");
			dos.write("期末成绩:" + (int) stu[i].getQiMoScore() + "\r\n");
			dos.write("综合成绩:" + (int) stu[i].getZongHeScore() + "\r\n");
			
			dos.close();
        }
		 
	}
	catch (IOException e)
	{  
		e.printStackTrace();  
	}
	finally
	{  
		try
		{  
			if (fr != null)  
				fr.close();  
			if (br != null)  
				br.close();  
		}
		catch (IOException ex)
		{  
			ex.printStackTrace();  
		}  
	}  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

}


  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/940712
推荐阅读
相关标签
  

闽ICP备14008679号