有一个学生类
private String name;
private int chinese;
private int math;
private int en;
private int sum;
public Student(String name,int chinese,int math,int en)
{
this.name=name;
this.chinese=chinese;
this.math=math;
this.en=en;
sum=chinese+math+en;
}
在定义一个操作学生的类,
public static Set<Student> getStudents(Comparator<Student>cmp) throws IOException
{
//键盘录入学生信息
System.out.println("请输入学生成绩:");
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
String line=null;
Set<Student> stus=null;
if(cmp==null)
stus=new TreeSet<Student>();
else
stus=new TreeSet<Student>(cmp);
while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
String[]info=line.split(",");
Student stu=new Student(info[0],Integer.parseInt(info[1]),Integer.parseInt(info[2]),Integer.parseInt(info[4]));
stus.add(stu);
}
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();
}
在测试的时候报:java.lang.ArrayIndexOutOfBoundsException数组下标异常,什么原因?
|