A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李娟 黑马帝   /  2011-12-23 23:37  /  1830 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李娟 于 2011-12-24 09:16 编辑

while((line=bufr.readLine())!=null)
                {
                        if("over".equals(line))
                                break;
                        String[] info = line.split(",");
                        String stu = new Student(info[0],Integer.parseInt(info[1]),
                                Integer.parseInt(info[2]),
                                Integer.parseInt(info[3]));
                        stus.add(stu);
                }
                bufr.close();
                return stus;
        }
        public static void write2File(Set<Student> stus)throws IOException
        {
                BufferedWriter bufw = new BufferedWriter(new FileWriter("stuinfo.txt"));
                for(Student stu:stus)
                {
                        bufw.write(stu.toString()+"\t");
                        bufw.write(stu.getSum()+"");
                        bufw.newLine();
                        bufw.flush();
                }
                bufw.close();
        }
}
class StudentInfoTest
{
        public static void main(String[] args) throws IOException
        {
                Comparator<Student> cmp = Collection.reverseOrder();
                Set<Student> stus = StudentInfoTool.getStudents();
                StudentInfoTool.write2File(stus);
        }
}

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

2 个回复

正序浏览
楼主贴上来的代码不全啊, 没法调试。
不过我发祥 Comparator<Student> cmp = Collection.reverseOrder();  应该改成
Comparator<Student> cmp = Collections.reverseOrder();

下面是我的看视频的时候的自己写的
import java.util.*;
import java.io.*;

public class ScoreOrder {
       
        /**
         * @param args
         */
        public static void main(String[] args) {
                Comparator<Student> cmp = Collections.reverseOrder();
                Set set  = getStudent( cmp);
                saveToFile(set);

        }
       
        public static void saveToFile(Set<Student> set){
                try {
                        BufferedWriter br = new BufferedWriter(new FileWriter("d:/test/score.txt"));
                        for(Student s:set){
                                br.write(s.toString());
                                br.newLine();
                        }
                        br.close();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
       
        public static Set<Student> getStudent(Comparator<Student> cmp) {
                Set studentSet;
                 if(cmp!=null){
                         studentSet = new TreeSet<Student>(cmp);
                 }else {
                         studentSet = new TreeSet<Student>();
                 }                           
                       
                        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String line = "";
                try {
                        while(true){
                                line = br.readLine();
                                if(line==null||"".equals(line)) break;
                                String[] arr = line.split(",");
                                Student s = new Student(arr[0],Integer.parseInt(arr[1]),Integer.parseInt(arr[2]),Integer.parseInt(arr[3]));
                                studentSet.add(s);
                        }
                        br.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }
               
                return studentSet;
        }
}

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
  String stu = new Student(info[0],Integer.parseInt(info[1]),
这里用String去接收Student对象似乎有点不妥。你应该是把Strudent错写为String了。
找不到符号的原因,你看看有没有写错对象名,或者方法名。如果确定这两个都没错,那就再看看有没有导入运行时需要的包。应该是需要java.util.*;

评分

参与人数 1技术分 +1 收起 理由
杨强 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马