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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李涛兴 中级黑马   /  2013-1-15 19:23  /  2025 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李涛兴 于 2013-1-16 10:34 编辑
  1. class StudentInfoTool{
  2.         public static Set<Student>getStudents()throws IOException
  3.         {
  4.                 BufferedReader bufr=
  5.                         new BufferedReader(new InputStreamReader(System.in));
  6.                 String line=null;
  7.                 Set<Student> stus =new TreeSet<Student>();
  8.                 while((line=bufr.readLine())!=null){
  9.                         if("over".equals(line)){
  10.                         break;
  11. }
  12.                         //String [] info =line.split(",");
  13.                         Student stu= new Student(info[0],Integer.parseInt(info[1]),
  14.                                         Integer.parseInt(info[1]),
  15.                                         Integer.parseInt(info[1]));
  16.                         
  17.                         stus.add(stu);
  18.                 }
  19.                 bufr.close();
  20.                 return stus;
  21.         }
  22.         
复制代码
注释的地方有问题 不知道问题出在哪,请指教下

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

8 个回复

倒序浏览
不通过编译,产生的异常贴出来..... LZ 导入 集合类没?
回复 使用道具 举报
逗号是一个字符,它是无法从string转换为char[]的!!
把双引号换成单引号即可。
  1. String [] info =line.split(’,‘);
复制代码
回复 使用道具 举报
赵文 发表于 2013-1-15 19:30
不通过编译,产生的异常贴出来..... LZ 导入 集合类没?

我是用myeclipse编译的,包肯定都导了,这是源代码。
  1. package day21;

  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.Set;
  8. import java.util.TreeSet;

  9. public class Student implements Comparable<Student>{

  10.         private String name;
  11.         private int ma,cn,en;
  12.         private int sum;
  13.        
  14.         Student(String name,int ma,int cn,int en){
  15.                 this.name=name;
  16.                 this.ma=ma;
  17.                 this.cn=cn;
  18.                 this.en=en;
  19.                 sum=ma+cn+en;
  20.         }

  21.         public int compareTo(Student s){
  22.                 int num =new Integer(this.sum).compareTo(new Integer(s.sum));
  23.                 if(num==0)
  24.                         return this.name.compareTo(s.name);
  25.                 return num;
  26.         }
  27.         public String getName() {
  28.                 return name;
  29.         }


  30.         public int getSum() {
  31.                 return sum;
  32.         }
  33.         public int hasCode(){
  34.                 return name.hashCode()+sum*78;
  35.                
  36.         }
  37.         public boolean equals(Object obj){
  38.                 if(!(obj instanceof Student ))
  39.                         throw new ClassCastException("类型补匹配");
  40.                 Student s=(Student)obj;
  41.                 return this.equals(s.name)&&this.sum==s.sum;
  42.         }
  43.        
  44.         public String toString(){
  45.                 return "Student["+name+","+ma+","+cn+","+en+"]";
  46.                
  47.         }
  48. }

  49. class StudentInfoTool{
  50.         public static Set<Student>getStudents() throws IOException
  51.         {
  52.                 BufferedReader bufr=
  53.                         new BufferedReader(new InputStreamReader(System.in));
  54.                 String line=null;
  55.                 Set<Student> stus =new TreeSet<Student>();
  56.                 while((line=bufr.readLine())!=null){
  57.                         if("over".equals(line));
  58.                         break;
  59.                         String [] info =line.split(",");
  60.                         Student stu= new Student(info[0],Integer.parseInt(info[1]),
  61.                                         Integer.parseInt(info[1]),
  62.                                         Integer.parseInt(info[1]));
  63.                        
  64.                         stus.add(stu);
  65.                 }
  66.                 bufr.close();
  67.                 return stus;
  68.         }
  69.        
  70.         public static void write2File(Set<Student> stus) throws IOException{
  71.                 BufferedWriter bufw=
  72.                         new BufferedWriter (new FileWriter("stuinfo.txt"));
  73.                 for(Student stu:stus){
  74.                         bufw.write(stu.toString()+"\t");
  75.                         bufw.write(stu.getSum());
  76.                         bufw.newLine();
  77.                         bufw.flush();
  78.                 }
  79.                 bufw.close();
  80.         }
  81.        
  82. }

  83. class StudentInfoTest{
  84.         public static void main(String[] args) throws IOException {
  85.                
  86.                 Set<Student>stus =StudentInfoTool.getStudents();
  87.                 StudentInfoTool.write2File(stus);
  88.         }
  89.        
  90. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
林嘉健 发表于 2013-1-15 19:35
逗号是一个字符,它是无法从string转换为char[]的!!
把双引号换成单引号即可。 ...

你说的方法不行啊
回复 使用道具 举报
把你第9行if语句后面的分号去掉吧

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
jonn 高级黑马 2013-1-15 20:06:22
7#
李涛兴 发表于 2013-1-15 19:40
我是用myeclipse编译的,包肯定都导了,这是源代码。

修改好了,下面是正确代码,LZ请查看 :D

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Set;
  7. import java.util.TreeSet;

  8. class Student implements Comparable<Student>{

  9.         private String name;
  10.         private int ma,cn,en;
  11.         private int sum;
  12.         
  13.         Student(String name,int ma,int cn,int en){
  14.                 this.name=name;
  15.                 this.ma=ma;
  16.                 this.cn=cn;
  17.                 this.en=en;
  18.                 sum=ma+cn+en;
  19.         }

  20.         public int compareTo(Student s){
  21.                 int num =new Integer(this.sum).compareTo(new Integer(s.sum));
  22.                 if(num==0)
  23.                         return this.name.compareTo(s.name);
  24.                 return num;
  25.         }
  26.         public String getName() {
  27.                 return name;
  28.         }


  29.         public int getSum() {
  30.                 return sum;
  31.         }
  32.         public int hasCode(){
  33.                 return name.hashCode()+sum*78;
  34.                
  35.         }
  36.         public boolean equals(Object obj){
  37.                 if(!(obj instanceof Student ))
  38.                         throw new ClassCastException("类型补匹配");
  39.                 Student s=(Student)obj;
  40.                 return this.equals(s.name)&&this.sum==s.sum;
  41.         }
  42.         
  43.         public String toString(){
  44.                 return "Student["+name+","+ma+","+cn+","+en+"]";
  45.                
  46.         }
  47. }

  48. class StudentInfoTool{
  49.         public static Set<Student> getStudents() throws IOException
  50.         {
  51.                 BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
  52.                 String line=null;
  53.                                 String[] info=null;
  54.                 Set<Student> stus =new TreeSet<Student>();
  55.                 while((line=bufr.readLine())!=null){
  56.                         if("over".equals(line)){
  57.                         break;
  58.                                                 }
  59.                         info=line.split(",");
  60.                         Student stu= new Student(info[0],Integer.parseInt(info[1]),
  61.                                         Integer.parseInt(info[1]),
  62.                                         Integer.parseInt(info[1]));
  63.                         
  64.                         stus.add(stu);
  65.                 }
  66.                 bufr.close();
  67.                 return stus;
  68.         }
  69.         
  70.         public static void write2File(Set<Student> stus) throws IOException{
  71.                 BufferedWriter bufw=
  72.                         new BufferedWriter (new FileWriter("stuinfo.txt"));
  73.                 for(Student stu:stus){
  74.                         bufw.write(stu.toString()+"\t");
  75.                         bufw.write(stu.getSum());
  76.                         bufw.newLine();
  77.                         bufw.flush();
  78.                 }
  79.                 bufw.close();
  80.         }
  81.         
  82. }

  83. public class StudentInfoTest{
  84.         public static void main(String[] args) throws IOException {
  85.                
  86.                 Set<Student>stus =StudentInfoTool.getStudents();
  87.                 StudentInfoTool.write2File(stus);
  88.         }
  89.         
  90. }
复制代码
回复 使用道具 举报
注释注释注释
回复 使用道具 举报
6楼说的对
Unreachable code 一般都是代码写在了不该出现的位置。如果在第10行提示错误就更明显了,break没有写在合适的位置,在while外面了。
最好while,if只有一行代码也加括号,这样比较好

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马