本帖最后由 唐志海 于 2014-1-16 23:47 编辑
- class student implements Comparable<student>
- {
- private String name;
- private int xx,mm,dd;
- private int sum;
- student(String name,int xx,int mm,int dd)
- {
- this.name=name;
- this.xx=xx;
- this.mm=mm;
- this.dd=dd;
- sum=xx+mm+dd;
- }
- public int compareTo(student s)
- {
-
- int num=new Integer(this.sum).compareTo(new Integer(s.name));
- if (num==0)
- return this.name.compareTo(s.name);
- return num;
- }
- public int hashCode()
- {
- return name.hashCode()+sum;
- }
- public boolean equals(Object obj)
- {
- if(!(obj instanceof student))
- try {
- throw new Exception("类型不匹配");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- student s=(student)obj;
- return this.name.equals(s.name)&&this.sum==s.sum;
-
- }
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getXx() {
- return xx;
- }
- public void setXx(int xx) {
- this.xx = xx;
- }
- public int getMm() {
- return mm;
- }
- public void setMm(int mm) {
- this.mm = mm;
- }
- public int getDd() {
- return dd;
- }
- public void setDd(int dd) {
- this.dd = dd;
- }
- public int getSum() {
- return sum;
- }
- public String toString()
- {
- return "student["+name+","+xx+","+mm+","+dd+"]";
- }
- public void setSum(int sum) {
- this.sum = sum;
- }
-
-
- }
- class student_tool
- {
- public static Set<student> getStudent() throws IOException
- {
- BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
- TreeSet<student> ts=new TreeSet<student>();
- String line=null;
- while((line=bf.readLine())!=null)
- {
- if(line.equals("over"))
- break;
- String[] ss=line.split(",");
- student stu=new student(ss[0],
- Integer.parseInt(ss[1]),
- Integer.parseInt(ss[2]),
- Integer.parseInt(ss[3]));
- ts.add(stu);
-
- }
- bf.close();
- return ts;
- }
- public static void writeTo(Set<student> s) throws IOException
- {
- BufferedWriter fw=new BufferedWriter(new FileWriter("D:\\studenInfo.txt"));
- for(student ss:s)
- {
- fw.write(ss.toString());
-
- fw.write(ss.getSum());
- fw.newLine();
- fw.flush();
-
- }
- fw.close();
- }
- }
- public class Text
- {
- public static void main(String[] args) throws IOException
- {
- Set<student> ss=student_tool.getStudent();
- student_tool.writeTo(ss);
-
- }
- }
复制代码 为什么编译通过后录入信息会出现java.lang.NumberFormatException:异常呢?? |
|