将本班学生的信息(每个学生至少包括学号、姓名、科目成绩(至少两门)、学分、平均分),存放于文件stduent.txt中。
  A.要求编写函数fun1对10个学生按某科成绩升序或降序(可选择)排序,并将结果(包括名次、学号、姓名、成绩、学分、平均分)存放于文件score.txt中。
  B.编写函数fun2,用二分法查找score.txt中某科某个成绩,并将结果打印出来。-------->以上是要求,可是我学得不好,处理名字啊,成绩啊什么的总是空指针错误或者其他错误,而且文件也不会啊,哪位高手能帮帮我呢?期末要交了!

解决方案 »

  1.   

    student.txt中数据的分割符是啥啊?
    先readline,再对每行的数据进行分析。用stringtokenizer去做
      

  2.   

    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    public class TextClass {
    public static void main(String[] args) {
    fun1();
    Student student = fun2(98);
    System.out.println(student.getName());

    }
    public static void fun1(){
    List<Student> studentList = new ArrayList<Student>(); 
    try {
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    new FileInputStream("student.txt"),"utf-8"));
    String str;
    while((str = in.readLine())!=null){
    if(str.startsWith("#")){
    continue;
    }
    System.out.println(str);
    String[] strs = str.split(":");
    //这里不习惯用数组来做,所以定义了一个学生信息类,在下面。
    Student student = new Student();
    student.setId(strs[0]);
    student.setName(strs[1]);
    student.setScore1(Integer.parseInt(strs[2]));
    student.setScore2(Integer.parseInt(strs[3]));
    student.setCredit(Integer.parseInt(strs[4]));
    student.setAvgScore(Double.parseDouble(strs[5]));
    studentList.add(student);
    }
    Collections.sort(studentList, new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
    return o1.getScore1()-o2.getScore1();
    //用语文成绩进行比较。
    }
    });
    PrintWriter out = new PrintWriter(
            new OutputStreamWriter(
                new BufferedOutputStream(
                    new FileOutputStream("score.txt")), "utf-8"));
    for(Student student:studentList){
    int i = 1;
    String scoreStr = "第"+ i++ + "名"+ ":"+student.getId()+":"
    +student.getName()+":"+student.getScore1()+":"+student.getScore2()
    +":"+student.getCredit()+":"+student.getAvgScore();
    out.println(scoreStr);
    out.flush();
    }
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    //des 为要查找的分数 找到之后你要干嘛你自己做写吧
    public static Student fun2(int des){
    //从score.txt中读取成绩等信息以便进行二分查找****************
    List<Student> studentList = new ArrayList<Student>(); 
    try {
    BufferedReader in = new BufferedReader(
    new InputStreamReader(
    new FileInputStream("score.txt"),"utf-8"));
    String str;
    while((str = in.readLine())!=null){
    if(str.startsWith("#")){
    continue;
    }
    String[] strs = str.split(":");
    Student student = new Student();
    student.setId(strs[1]);
    student.setName(strs[2]);
    student.setScore1(Integer.parseInt(strs[3]));
    student.setScore2(Integer.parseInt(strs[4]));
    student.setCredit(Integer.parseInt(strs[5]));
    student.setAvgScore(Double.parseDouble(strs[6]));
    studentList.add(student);
    }
    //开始进行二分查找**************************
    int low = 0;
    int high = studentList.size()-1;
    while(low <= high) {
    int middle = (low + high)/2;
    if(des == studentList.get(middle).getScore1()) {
    return studentList.get(middle);
    }else if(des <studentList.get(middle).getScore1()) {
    high = middle - 1;
    }else {
    low = middle + 1;
    }
    }
    return null;
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    }

    }class Student{
    private String id;
    private String name;
    private int score1;//语文成绩
    private int score2;//数学成绩
    private int credit;//学分
    private double avgScore;


    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 int getScore1() {
    return score1;
    }
    public void setScore1(int score1) {
    this.score1 = score1;
    }
    public int getScore2() {
    return score2;
    }
    public void setScore2(int score2) {
    this.score2 = score2;
    }
    public int getCredit() {
    return credit;
    }
    public void setCredit(int credit) {
    this.credit = credit;
    }
    public double getAvgScore() {
    return avgScore;
    }
    public void setAvgScore(double avgScore) {
    this.avgScore = avgScore;
    }
    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    return result;
    }
    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    Student other = (Student) obj;
    if (id == null) {
    if (other.id != null)
    return false;
    } else if (!id.equals(other.id))
    return false;
    return true;
    }

    }
      

  3.   

    就这样了,认真看一下,还是能看的懂的。不懂的方法,可以查看API。不懂再问吧
      

  4.   

    对了,你的那两个文件要用你自己的绝对路径。我写的时候是把那两个文件放在和类文件一起的所以就直接写名字。如果出现了文件空指针异常的话,要自己改写好路径。在Myeclipse里面新建一个工程,把两个Txt文件,直接放到工程里面,就可以像我那样直接引用。