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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© HeiMaYSL 中级黑马   /  2012-5-21 11:09  /  2759 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

红色地方编译出错了?求解?

import java.io.*;
import java.util.*;
class test
{
public static void main(String[] args) throws IOException
{
  Comparator<Student> cmp=Collections.reverseOrder();
  Set<Student> stus=StudentTool.getStudents(cmp);
  StudentTool.writeFile(stus);
}
}
//定义工具类
class StudentTool
{
//定义一个没有比较器的方法。
public static Set<Student> getStudents() throws IOException
{
  return getStudents(null);
}
//定义了一个有比较器的读取数据的方法。
public static Set<Student> getStudents(Comparator<Student> cmp) throws IOException
{
  //信息从控制台上获取。
   BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
   String line=null; //初始化,用来
   Set<Student> stus=null;  //创建set集合引用。
   if(cmp==null)  //如果比较器为null,
    stus=new TreeSet<Student>();  //就创建一个没有比较器的集合
   else
    stus=new TreeSet<Student>(cmp);  //如果不为空,就创建一个有比较器的集合。
  //一行一行读取从控制台输入的数据,如果不为空,就循环读取
   while ((line=bufr.readLine())!=null)
   {
    if("over".equals(line));  //如果读取到over 就停止
    break;
  String[] infos=line.split(",");  //在读取中一","为分割符,对读出的数据进行分割。
   //把分割后的数据和Student类中的数据结合起了。
   Student stu=new Student(infos[0],Integer.parseInt(infos[1]),
                                Integer.parseInt(infos[2]),
           Integer.parseInt(infos[3]));
      
   
   stus.add(stu);//把读取出来 的数据添加到set集合中。
   }
   bufr.close();  //关闭资源
   return stus; //返回从控制台输入的数据添加到集合后的集合。
}
    public static void writeFile(Set<Student> stus) throws IOException
{
  //常见缓存区,和文件关联
  BufferedWriter bufw=new BufferedWriter(new FileWriter("studentinfo.txt"));
  for (Student stu : stus )
  {
   bufw.write(stu.toString()+"\t");
   bufw.write(stu.getSum()+"");
   bufw.newLine();
   bufw.flush();
  }
  bufw.close();
}
}

//描述学生类
class Student implements Comparable<Student> //实现接口Comparble就一定要是重写compareTo()方法
{
private String name;
private int ma,cn,en;
private int sum;
Student(String name,int ma,int cn,int en)
{
  this.name=name;
  this.ma=ma;
  this.cn=cn;
  this.en=en;
  sum=ma+cn+en;
}
public int compareTo(Student s)
{
  int num=new Integer(this.sum).compareTo(new Integer(s.sum));
  if(num==0)  //如果分数相同就比较姓名
   return this.name.compareTo(s.name);
  return num; //返回比较结果:正数,负数或者0。
}
public String getName()
{
  return name;
}
public int getSum()
{
  return sum;
}
//复该每个元素的哈希值,让每一个哈希值唯一。
public int hashCode()
{
  return name.hashCode()+sum*23;
}
    public boolean equals(Object obj)
{
  if(!(obj instanceof Student))  //传入的类如果不是Student类型的,就捕获异常
   throw new ClassCastException("类型不匹配");  //该异常是runtimeException的异常子类。
  //如果是
  Student s=(Student)obj; //就把Object类型强转成Student类型
  return this.name.equals(s.name) && this.sum==s.sum;  //返回比较姓名和总分的结果;
}
    //覆盖类中的toString方法
public String toString()
{
  return "Student["+name+","+ma+","+cn+","+en+"]";
}
}

编译错误结果:

点评

像这种问题,提问的时候前面把需求写上,看代码就容易多了,亲  发表于 2012-5-21 12:19

6 个回复

倒序浏览
{
    if("over".equals(line));  //如果读取到over 就停止
    break;
  String[] infos=line.split(",");  //在读取中一","为分割符,对读出的数据进行分割。
   //把分割后的数据和Student类中的数据结合起了。

把分号去掉就好了。

评分

参与人数 1技术分 +1 收起 理由
贠(yun)靖 + 1

查看全部评分

回复 使用道具 举报
你能告诉我你做这个程序的需求是什么,我有点看不懂。。。。
回复 使用道具 举报
伊文龙 发表于 2012-5-21 11:18
{
    if("over".equals(line));  //如果读取到over 就停止
    break;

:)谢了哥们儿。就是这里的问题,我太马虎了。居然出现这样的错误,惭愧啊!
我找了好长时间没有找到错误,
谢谢你了啊!
回复 使用道具 举报
伊文龙 发表于 2012-5-21 11:19
你能告诉我你做这个程序的需求是什么,我有点看不懂。。。。

这个是IO教程视频里的最后一个视频中的一个小练习题。
需求是这样的:
回复 使用道具 举报
哦哦,谢谢,我还没看到呢,。回头我也试试写写。
问题解决了吧。
回复 使用道具 举报
解决了。{:soso_e100:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马