如何按年级对文件进行排序?

时间:2013-03-03 17:38:00

标签: java arrays file sorting

这是一个按ID排序文件的完整程序。但是,我想按年级排序。我修改了几次,但似乎没有用。有人可以指示我在哪里更改ID来评分。另外,您认为此代码可以简化,还是比其他代码更简单。

对于不良缩进感到抱歉,此源代码也可以找到here

student.txt文件:

4 A 87 A
5 B 99 A+
1 C 75 A
2 D 55 C
3 E 68 B

源:

import java.io.*;
import java.util.*;

    class ShowData implements Comparable {
    int id;
    String name;
    int marks;
    String grade;

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

    public int getId() {
            return id;
    }

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

    public String getName() {
            return name;
    }

    public void setMarks(int marks) {
            this.marks = marks;
    }

    public int getMarks() {
            return marks;
    }

    public void setGrade(String grade) {
            this.grade = grade;
    }

    public String getGrade() {
            return grade;
    }

    public int compareTo(Object Student) throws ClassCastException {
            if (!(Student instanceof ShowData))
                    throw new ClassCastException("Error");
            int ide = ((ShowData) Student).getId();
            return this.id - ide;
    }
    }



public class SortFile {
    SortFile() {
    int j = 0;
    ShowData data[] = new ShowData[5];
    try {
    FileInputStream fstream = new FileInputStream("C:/student.txt");
    // Use DataInputStream to read binary NOT text.
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    ArrayList list = new ArrayList();
    while ((strLine = br.readLine()) != null) {
    list.add(strLine);
    }

Iterator itr;
    for (itr = list.iterator(); itr.hasNext();) {
    String str = itr.next().toString();
    String[] splitSt = str.split(" ");
    String id = "", name = "", marks = "", grade = "";
    for (int i = 0; i < splitSt.length; i++) {
    id = splitSt[0];
    name = splitSt[1];
    marks = splitSt[2];
    grade = splitSt[3];
    }
    data[j] = new ShowData();
    data[j].setId(Integer.parseInt(id));
    data[j].setName(name);
    data[j].setMarks(Integer.parseInt(marks));
    data[j].setGrade(grade);
    j++;
    }

Arrays.sort(data);
    File file = new File("C:/new.txt");
    FileWriter fw = new FileWriter(file, true);
    BufferedWriter out = new BufferedWriter(fw);
    System.out.println("********Sorted by id********");
    String strVal = "";
    for (int i = 0; i < 5; i++) {
    ShowData show = data[i];
    int no = show.getId();
    String name = show.getName();
    int marks = show.getMarks();
    String grade = show.getGrade();
    System.out.println(no + " " + name + " " + marks + " " + grade);
    String d = no + " " + name + " " + marks + " " + grade;
    ArrayList al = new ArrayList();
    al.add(d + "\n");
    Iterator itr1 = al.iterator();
    while (itr1.hasNext()) {
    out.write(itr1.next().toString());
    out.newLine();
    }
    }
    out.close();
    } catch (Exception e) {
    }
    }
public static void main(String[] args) {
            SortFile data = new SortFile();
    }
    }

1 个答案:

答案 0 :(得分:0)

compareTo()方法目前正在比较ID,而不是等级。试着比较一下 而是标记。