请大家帮忙看看代码:运行时候一直说数组角标越界异常 ,我看了几遍没找到问题所在,请指教
package com.itheima;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.TreeSet;
public class StudentDemo {
/**
* 需求:从键盘录入学生信息如:zhangsan,60,50,40
* 将学生信息存入文件F:\\stu.txt中,并按照总分高低由高到低排序
* 步骤:1建立学生类,具有:姓名三科成绩总分五个成员变量,因为需要排序
* 所以实现接口比较器implements Compareable<Student>
* 复写compareTo方法 和toString方法因为学生信息存储可以使用treeSet集合
* 2 创建一个学生工具类,将键盘输入的学生信息装入treeSet集合中,
* 3创建一个类将集合数据写入指定文件中
*
*
*
*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Student> ts=StudentInfo.getStudent();
new Write2File(ts).writeStudent();
}
}
class Write2File{
private TreeSet<Student> ts;
Write2File(TreeSet<Student> ts){
this.ts=ts;
}
public void writeStudent(){
BufferedWriter bufw=null;
Iterator<Student> it = ts.iterator();
while(it.hasNext()){
try {
bufw=new BufferedWriter(new FileWriter("F:\\stu.txt"));
bufw.write(it.next().toString());
bufw.newLine();
bufw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
if(bufw!=null)
bufw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class StudentInfo{
public static TreeSet<Student> getStudent(){
BufferedReader bufr =new BufferedReader(new InputStreamReader(System.in));
String line =null;
TreeSet<Student> ts=new TreeSet<Student>();
try {
while((line=bufr.readLine())!=null){
String[] info =new String[1024];
int i=0;
while(line!=null){
if("over".equals(line))
break;
else if(",".equals(line))//可以使用line.split切换
continue;
/*
* String[] info=line.split(",");
* Interger.pareseInt(String)
* 可以将String类型转换成整型
* */
else info[i++]=line;
}
ts.add(new Student(info[0],Integer.parseInt(info[1]),
Integer.parseInt(info[2]),Integer.parseInt(info[3])));
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bufr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return ts;
}
}
class Student implements Comparable<Student>{
private String name;
private int num;
private int math,english,chinese;
Student(String name,int math,int english,int chinese){
this.name=name;
this.math=math;
this.english=english;
this.chinese=chinese;
this.num=math+english+chinese;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
@Override
public int compareTo(Student s) {
if((this.num-num)==0)
return this.name.compareTo(s.name);
return this.num-s.num;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + num;
return result;
}
@Override
public String toString() {
return name +" " + chinese + " " + english
+ " " + math + " " + num ;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num != other.num)
return false;
return true;
}
}
|