import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class SortSex {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
File f1=new File("c:\\Users\\11202\\Desktop\\学生信息.txt");
File f2=new File("c:\\Users\\11202\\Desktop\\aaa.txt");
FileReader fr = new FileReader(f1);
BufferedReader cin=new BufferedReader(fr);
FileWriter fw = new FileWriter(f2);
BufferedWriter cout=new BufferedWriter(fw);
Student[] student=new Student[1000];
String s="";int i=0;
while((s=cin.readLine())!=null)
{
student[i]=new Student();
String[] a=new String[6];
a=s.split(" ");
student[i].setName(a[0]);;
student[i].setNum(a[1]);
student[i].setSex(a[2]);;
student[i].setGrade(a[3]);
student[i].setBanji(a[4]);
student[i].setScore(a[5]);
i++;
}
for(int t=0;t<student.length;t++){
if(student[t].getSex()=="男"){
cout.write(student[t].getStudent());
}
}
fr.close();
fw.close();
}
}
Exception in thread "main" java.lang.NullPointerException
at SortSex.main(SortSex.java:37)
注:
public class Student {
private String name;
private String num;
private String sex;
private String grade;
private String banji;
private String score;
public Student(String name, String num, String sex, String grade, String banji, String score)
{
this.name = name;
this.num = num;
this.sex = sex;
this.grade = grade;
this.banji = banji;
this.score = score;
}
public Student()
{
this.name = "未知";
this.num = "未知";
this.sex = "未知";
this.grade = "未知";
this.banji = "未知";
this.score = "未知";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getBanji() {
return banji;
}
public void setBanji(String banji) {
this.banji = banji;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getStudent() {
return name+" "+num+" "+sex+" "+grade+" "+banji+" "+score;
}
}