package count;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Count {
public static void main(String[] args) {
String filepath = System.getProperty("user.dir") + File.separator
+ "file" + File.separator + "student.txt";
Message messages[] = new Message[20000];
int index = 0;
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(
new File(filepath)));
String s = "";
while ((s = bufferedReader.readLine()) != null) {
String[] infos = s.split(",");
Message message = new Message();
message.setClassID(infos[0]);
message.setId(infos[1]);
message.setName(infos[2]);
message.setSex(infos[3]);
message.setSubject(infos[4]);
message.setCollege(infos[5]);
messages[index] = message;
index++;
}
/*******************************************/
College[] colleges=new College[30];
int collegeindex=0;
for(int i=0;i<index;i++){
Message message=messages[i];
String collegesName=message.getCollege();
boolean flag=false;
for(int j=0;j<collegeindex;j++){
if(colleges[j].getName().equals(collegesName)){
if(message.getSex().equals("男")){
colleges[j].maleadd();
}else{
colleges[j].femaleadd();
}
flag=true;
break;
}
}
if(!flag){
College college=new College(collegesName);
colleges[collegeindex]=college;
collegeindex++;
if(message.getSex().equals("男")){
college.maleadd();
}else{
college.femaleadd();
}
}
}
for(int i=0;i<collegeindex;i++){
System.out.println("name "+colleges[i].getName()
+" 男生:"+ colleges[i].getMaleCount()
+" 女生:"+ colleges[i].getFemaleCount()
+" 比值:"+ colleges[i].bili());
}
// int maleCount = 0;
// for(int i=0;i<index;i++){
// System.out.println(messages[i]);//syso+alt+? 快捷打
// if(messages[i].getSex().equals("男")){
// maleCount++;
// }
// }
// int femaleCount = index+1 - maleCount;//数组下标加1为总人数
// System.out.println("total:" + index +1 +"男生:"+maleCount+"女生:"+femaleCount+"男女比例:"+((double)maleCount/(double)femaleCount));
//
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package count;
public class Message {
private String classID;//班级
private String id;//学号
private String name;//姓名
private String sex;//性别
private String subject;//专业
private String college;//学院
public String getClassID() {
return classID;
}
public void setClassID(String classID) {
this.classID = classID;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
@Override
public String toString() {
return "Message [classID=" + classID + ", id=" + id + ", name=" + name
个人中心
+ ", sex=" + sex + ", subject=" + subject + ", college="
+ college + "]";
}
}
/*
链接的本地txt信息自备
*/