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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陌路行者 中级黑马   /  2013-7-11 17:16  /  753 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陌路行者 于 2013-7-11 18:02 编辑
  1. import java.io.*;
  2. import java.util.*;
  3. class Student implements Comparable<Student>
  4. {
  5.         private String name;
  6.         private String sex;
  7.         private int java,c_shar,php;
  8.         private int sum;
  9.         private int avg;
  10.         Student(String name,String sex,int java,int c_shar,int php)
  11.         {
  12.                 this.name = name;
  13.                 this.sex = sex;
  14.                 this.java = java;
  15.                 this.c_shar = c_shar;
  16.                 this.php = php;
  17.         }
  18.         public int compareTo(Student s)
  19.         {
  20.                 int num = new Integer(this.sum).compareTo(new Integer(s.sum));
  21.                 if(num==0)
  22.                         this.name.equals(s.name);
  23.                 return num;
  24.         }
  25.         public int getSum()
  26.         {
  27.                 return sum = java+c_shar+php;
  28.         }
  29.         public int getAvg()
  30.         {
  31.                 return sum/3;
  32.         }
  33.         public boolean equalse(Object obj)
  34.         {
  35.                 if(!(obj instanceof Student))
  36.                         throw new ClassCastException("类型不匹配");
  37.                 Student s = (Student)obj;
  38.                 return this.name.equals(s.name) && this.sum==s.sum;
  39.         }
  40.         public int hashCode()
  41.         {
  42.                 return name.hashCode()+sum*78;
  43.         }
  44.         public String toString()
  45.         {
  46.                 return "Student["+java+","+c_shar+","+php+"]";
  47.         }
  48. }
  49. class StudentInfoToos
  50. {
  51.         public static Set<Student> getStudnetInfo()throws IOException
  52.         {
  53.                 BufferedReader bufr =
  54.                         new BufferedReader(new InputStreamReader(System.in));</p><p>System.out.println("请输入学生基本信息");
  55.                 String line = null;
  56.                 Set<Student> stus = new TreeSet<Student>();
  57.                 while((line=bufr.readLine())!=null)
  58.                 {
  59.                         if("over".equals(line))
  60.                                 break;
  61.                         String[] info = line.split(",");
  62.                         Student stu = new Student(info[0],info[1],
  63.                                 Integer.parseInt(info[2]),
  64.                                 Integer.parseInt(info[3]),
  65.                                 Integer.parseInt(info[4]));
  66.                         stus.add(stu);

  67.                 }
  68.                 bufr.close();
  69.                 return stus;
  70.         }
  71.         public static void write2File(Set<Student> stus)throws IOException
  72.         {
  73.                 BufferedWriter bufw = new BufferedWriter(new FileWriter("Info.txt"));
  74.                 for(Student stu : stus)
  75.                 {
  76.                         bufw.write(stu.toString()+'\t');
  77.                         bufw.write(stu.getSum()+'\t');
  78.                         bufw.write(stu.getAvg());
  79.                         bufw.newLine();
  80.                         bufw.flush();               
  81.                 }
  82.                 bufw.close();
  83.         }
  84. }
  85. class StudentInfo
  86. {
  87.         public static void main(String[] args)throws IOException
  88.         {
  89.                 Set<Student> stus = StudentInfoToos.getStudnetInfo();
  90.                 StudentInfoToos.write2File(stus);
  91.         }
  92. }
复制代码
怎么解决空指针异常,

1 个回复

倒序浏览
问题自己解决了,是程序的小BUG
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马