java文件学生_Java文件操作(求各专业第一名的学生)

import java.io.*;importjava.util.HashMap;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.Map;importjava.util.Set;public classPrintTheNo1 {public static void main(String [] args) throwsIOException

{try{

LinkedList stuList =getStuList();

HashMap map =getSumScoreOfStudent();

getTopStu(stuList,map);

}catch(IOException io)

{

System.out.println("出错了呀!"+io);

}

}//读取info文件

public static LinkedList getStuList() throwsIOException

{

String oneLine;try{

FileReader a= new FileReader("D:\\NewPro\\Files\\info.txt");

BufferedReader br= newBufferedReader(a);int line = 0;

LinkedList stuList = new LinkedList();while((oneLine = br.readLine() )!= null)

{//从文件的第二行开始读取数据

line++;if(line == 1) continue;else{

String[] info= oneLine.split(" ");

Student stu= new Student(Integer.parseInt(info[0]),(String)info[1],(String)info[2],(String)info[3]);

stuList.add(stu);

}

}for(Student s:stuList)

System.out.println(s.toString());

br.close();returnstuList;

}catch(IOException io)

{

System.out.println("error@"+io);return null;

}

}//求各个学生的总分

public static HashMap getSumScoreOfStudent() throwsIOException

{

HashMap map = new HashMap();

String oneLine;try{

FileReader a= new FileReader("D:\\NewPro\\Files\\score.txt");

BufferedReader br= newBufferedReader(a);int line = 0;double sum = 0;while((oneLine = br.readLine() )!= null)

{++line;if(line == 1) continue;else{

String [] info= oneLine.split(" ");int id = Integer.parseInt(info[0]);if(map.containsKey(id))

{

sum= map.get(id)+Double.parseDouble(info[2]);

map.put(id,sum);

}elsemap.put(id,Double.parseDouble(info[2]));

}

}returnmap;

}catch(IOException io)

{

System.out.println("error in score.txt"+io);return null;

}

}//各个专业第一名的学生

public static void getTopStu(LinkedList stuList,HashMapmap)

{

Student s= newStudent();int id =0;double score = 0;double maxScore = 0;

String major;//MajorAndScore保存专业--最高分

HashMap MajorAndScore = new HashMap();//result保存 专业--学号,然后遍历result,根据result的学号值找到Student信息,保存到topStudent链表中。

HashMap result = new HashMap();

LinkedList topStudent = new LinkedList();if(stuList.size() == 0 || map.size() ==0)return;for(int i=0;i

{

s=stuList.get(i);

id=s.getId();

score= map.get(id);//拿到该学生的总分

major =s.getMajor();if(map.containsKey(id) )

{if(MajorAndScore.containsKey(major) && score >maxScore)//如果已经有major.且当前学生的总分更大一些,把更大的分数加进去

{

maxScore=score;

MajorAndScore.put(major,maxScore);

result.put(major,id);

}else if(!MajorAndScore.containsKey(major))//如果不存在major key

{

MajorAndScore.put(major,score);

result.put(major,id);

}else{

map.remove(id);

}

}

}

@SuppressWarnings("rawtypes")

Set st=result.entrySet();

Iterator it=st.iterator();while(it.hasNext())

{

Map.Entry entry=(Map.Entry)it.next();

topStudent.add(getById(stuList,(Integer)entry.getValue()));

}

System.out.println("各专业第一名是: ");for(int i=0;i

{

System.out.println(topStudent.get(i).toString());

}

}//根据id找到学生信息

private static Student getById(LinkedList stuList,intid)

{for(int i=0;i

{if(stuList.get(i).getId() ==id)

{returnstuList.get(i);

}

}return null;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值