黑马程序员技术交流社区

标题: IO流练习中的一个问题 很迷惑 希望大神来帮帮我 [打印本页]

作者: wodenhaowzg    时间: 2013-10-13 17:21
标题: IO流练习中的一个问题 很迷惑 希望大神来帮帮我
import java.io.*;
import java.util.*;

class StudText
{
        public static void main(String[] args)throws IOException
        {
                Set<StudentCard> scc = StuTool.getStudent();
                StuTool.creFile(scc);
        }
}


//创建的学生对象
class Student implements Comparable<Student>
{
        private String name;
        private int math , yuwen , english; //三科成绩
        private int sum = 0;
        StudentCard(String name , int math , int lang , int forlang)
        {
                this.name = name;
                this.math = math;
                this.lang = lang;
                this.forlang = forlang;
                sum = math + lang + forlang; //总分
        }
        public int hashCode()
        {
                return name.hashCode() + sum*20;
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof StudentCard))
                        throw new RuntimeException("类型不匹配");
                StudentCard sc = (StudentCard)obj;
                return this.name.equals(name) && this.sum == sum;
        }
        public int compareTo(StudentCard sc)
        {
                int num = new Integer(this.sum).compareTo(new Integer(sum));
                        if(num == 0)
                                return this.name.compareTo(name);
                return num;
        }
        public int getSum()
        {
                return sum;
        }
        public String toString()
        {
                return "姓名:" +name + "        成绩:" +math + "," +yuwsen +"," + english;
        }
}

class StuTool
{
        public static Set<Student> getStudent() throws IOException
        {
                BufferedReader bur = new BufferedReader(new InputStreamReader(System.in));
                Set<Student>  stud = new TreeSet<Student>();
                String s = null;
                while((s = bur.readLine()) != null)
                {
                        if("over".equals(s))
                                break;
                        String[] arr = s.split(",");
                        Student  stu = new StudentCard(arr[0] , Integer.parseInt(arr[1]) ,
                                                                                            Integer.parseInt(arr[2]) ,
                                                                                            Integer.parseInt(arr[3]));
                        stud.add(stu);
                }
                bur.close();
                return stud;
        }

        public static void creFile(Set<Student> stud) throws IOException
        {
                BufferedWriter buw = new BufferedWriter(new FileWriter("stud.txt"));
                for(Student sc : stud)
                {
                        buw.write(sc.toString() + "\t");
                        buw.write(sc.getSum() + "");
                        buw.newLine();
                        buw.flush();
                }
                buw.close();
        }
}

跟视频对了好几遍,为什么生成的文件只有一行数据? 哪里有问题 郁闷



作者: wodenhaowzg    时间: 2013-10-13 17:50
。。。刚才发帖改把StudentCard名字改成Student了,所以可能其中有地方没改过来
new FileWriter("stud.txt",true) FileWriter的构造函数没这个。。
我在eclipse运行 没语法错误 但键盘输入了好多行 文件里就存了第一行
作者: To    时间: 2013-10-14 09:49
楼主你好,如果问题已解决请将帖子状态修改为提问结束,如果未解决请继续提问,谢谢合作
如果不会修改请看解释帖:http://bbs.itheima.com/thread-89313-1-1.html




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2