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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 唐志海 中级黑马   /  2014-1-16 22:40  /  1231 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 唐志海 于 2014-1-16 23:47 编辑
  1. class student implements Comparable<student>
  2. {
  3.         private String name;
  4.         private int xx,mm,dd;
  5.         private int sum;
  6.         student(String name,int xx,int mm,int dd)
  7.         {
  8.                 this.name=name;
  9.                 this.xx=xx;
  10.                 this.mm=mm;
  11.                 this.dd=dd;
  12.                 sum=xx+mm+dd;
  13.         }
  14.         public int compareTo(student s)
  15.         {
  16.                
  17.                 int num=new Integer(this.sum).compareTo(new Integer(s.name));
  18.                 if (num==0)
  19.                         return this.name.compareTo(s.name);
  20.                 return num;
  21.         }
  22.         public int hashCode()
  23.         {
  24.                 return name.hashCode()+sum;
  25.         }
  26.         public  boolean equals(Object obj)
  27.         {
  28.                 if(!(obj instanceof student))
  29.                         try {
  30.                                 throw new Exception("类型不匹配");
  31.                         } catch (Exception e) {
  32.                                 // TODO Auto-generated catch block
  33.                                 e.printStackTrace();
  34.                         }
  35.                 student s=(student)obj;
  36.                 return this.name.equals(s.name)&&this.sum==s.sum;
  37.                         
  38.         }
  39.         
  40.         public String getName() {
  41.                 return name;
  42.         }
  43.         public void setName(String name) {
  44.                 this.name = name;
  45.         }
  46.         public int getXx() {
  47.                 return xx;
  48.         }
  49.         public void setXx(int xx) {
  50.                 this.xx = xx;
  51.         }
  52.         public int getMm() {
  53.                 return mm;
  54.         }
  55.         public void setMm(int mm) {
  56.                 this.mm = mm;
  57.         }
  58.         public int getDd() {
  59.                 return dd;
  60.         }
  61.         public void setDd(int dd) {
  62.                 this.dd = dd;
  63.         }
  64.         public int getSum() {
  65.                 return sum;
  66.         }
  67.         public String toString()
  68.         {
  69.                 return "student["+name+","+xx+","+mm+","+dd+"]";
  70.         }
  71. public void setSum(int sum) {
  72.                 this.sum = sum;
  73.         }
  74.         
  75.         
  76. }
  77. class student_tool
  78. {
  79.          public static Set<student> getStudent() throws IOException
  80.          {
  81.                  BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
  82.                  TreeSet<student> ts=new TreeSet<student>();
  83.                  String line=null;
  84.                  while((line=bf.readLine())!=null)
  85.                  {
  86.                          if(line.equals("over"))
  87.                                  break;
  88.                          String[] ss=line.split(",");
  89.                          student stu=new student(ss[0],
  90.                                                                          Integer.parseInt(ss[1]),
  91.                                                                          Integer.parseInt(ss[2]),
  92.                                                                          Integer.parseInt(ss[3]));
  93.                          ts.add(stu);
  94.                         
  95.                  }
  96.                  bf.close();
  97.                  return ts;
  98.          }
  99.          public static void writeTo(Set<student> s) throws IOException
  100.          {
  101.                  BufferedWriter fw=new BufferedWriter(new FileWriter("D:\\studenInfo.txt"));
  102.                  for(student ss:s)
  103.                  {
  104.                          fw.write(ss.toString());
  105.                         
  106.                          fw.write(ss.getSum());
  107.                          fw.newLine();
  108.                          fw.flush();
  109.                         
  110.                  }
  111.                  fw.close();
  112.          }
  113. }
  114. public class Text
  115. {
  116.         public static void main(String[] args) throws IOException
  117.         {
  118.                 Set<student> ss=student_tool.getStudent();
  119.                 student_tool.writeTo(ss);
  120.                
  121.         }
  122. }
复制代码
为什么编译通过后录入信息会出现java.lang.NumberFormatException:异常呢??

3 个回复

倒序浏览
你的student类的构造的方法接收的参数类型是:String,int,int,int
所以你往控制台上输入 zhangsan, 1, 2, 3
后面三个必须是int型的,要不然就会出现你说的错误
回复 使用道具 举报
int num=new Integer(this.sum).compareTo(new Integer(s.name));

你这里是用一个整数和一个字符串进行比较,
s.name不能转换成整数,当然会出现数字格式异常
改成s.sum就对了
回复 使用道具 举报
§傻、才乖 发表于 2014-1-16 23:32
int num=new Integer(this.sum).compareTo(new Integer(s.name));

你这里是用一个整数和一个字符串进行比 ...

郁闷,写太快了都没有发现。。。谢谢了:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马