今天写了一个存学生信息的代码,一直报数组引用超出界限错误,大神帮忙给看看呀!万分感谢!
报错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.itpractice.StudentsInfoTool.getStudents(StudentsInfoDemo.java:58)
at com.itpractice.StudentsInfoDemo.main(StudentsInfoDemo.java:29)
代码如下,,报错误的两行已经用红颜色标出:
- <font color="#333333">package com.itpractice;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Set;
- import java.util.TreeSet;
- /*
- * 有五个学生,每个学生有三门课的成绩
- * 从键盘录入以上数据(包括姓名,三门功课成绩)
- * 键盘录入的格式:zhangsan,30.40,50 计算出总成绩
- * 把学生的信息和课程总分按照总分由高到低顺序存放在磁盘文件“stud.txt”中
- *
- * 思想:
- * 1)获取键盘录入的一行数据,将数据封装成学生对象
- * 2)鉴于需要排序,将学生对象放入TreeSet集合中
- * 3)将集合中的信息写入到一个文件中
- *
- */
- public class StudentsInfoDemo {
- public static void main(String[] args) {
- System.out.println("请输入学生信息(信息之间以逗号分隔):");
- </font><font color="#ff0000">Set<Students> studentSet = StudentsInfoTool.getStudents();</font><font color="#333333">
- System.out.println("***********成绩单***********");
- StudentsInfoTool.SaveInfo(studentSet);
- }
- }
- /**
- * 学生工具类 从键盘录入学生信息,存储到集合中,
- */
- class StudentsInfoTool {
- // 函数:将集合中的信息写入到一个文件中
- public static Set<Students> getStudents() {
- // 定义存储学生对象的Set集合
- Set<Students> sets = new TreeSet<Students>();
- // 键盘输入流
- BufferedReader bfr = new BufferedReader(
- new InputStreamReader(System.in));
- String line = null;
- try {
- // 读取输入流学生信息
- while ((line = bfr.readLine()) != null) {
- // 若输入over,则停止输入
- if ("over".equals(bfr))
- break;
- String[] info = line.split(",");
- </font><font color="#ff0000">Students stu = new Students(info[0], Integer.parseInt(info[1]),
- Integer.parseInt(info[2]), Integer.parseInt(info[3]));</font><font color="#333333">
- // 学生对象添加到Set集合中
- sets.add(stu);
- }
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (bfr != null)
- bfr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // 返回存储学生信息的Set集合
- return sets;
- }
- // 函数:将集合中的信息写入到一个文件中
- public static void SaveInfo(Set<Students> sets) {
- // 输出流
- BufferedWriter bfw = null;
- try {
- bfw = new BufferedWriter(new FileWriter("stud.txt"));
- bfw.write("学生姓名------数学------语文------英语");
- for (Students stu : sets) {
- bfw.write(stu.toString() + "\t");
- bfw.write(stu.getSum() + "");
- bfw.newLine();
- bfw.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (bfw != null)
- try {
- bfw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 学生类
- *
- * @author Administrator
- *
- */
- class Students implements Comparable {
- /**
- * 学生姓名
- */
- private String name;
- /**
- * 课程成绩
- */
- private int math;
- private int chinese;
- private int english;
- /**
- * 总成绩
- */
- private int sum;
- /**
- * 构造函数
- */
- public Students(String name, int math, int chinese, int english) {
- this.name = name;
- this.math = math;
- this.chinese = chinese;
- this.english = english;
- sum = math + chinese + english;
- }
- /**
- * getXxx()和setXxx()方法
- */
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getMath() {
- return math;
- }
- public void setMath(int math) {
- this.math = math;
- }
- public int getChinese() {
- return chinese;
- }
- public void setChinese(int chinese) {
- this.chinese = chinese;
- }
- public int getEnglish() {
- return english;
- }
- public void setEnglish(int english) {
- this.english = english;
- }
- /**
- * 获取学生总分
- */
- public int getSum() {
- return sum;
- }
- /**
- * 重写hashCode()方法
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + chinese;
- result = prime * result + english;
- result = prime * result + math;
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- result = prime * result + sum;
- return result;
- }
- /**
- * 重写equals()方法
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- Students other = (Students) obj;
- if (chinese != other.chinese)
- return false;
- if (english != other.english)
- return false;
- if (math != other.math)
- return false;
- if (name == null) {
- if (other.name != null)
- return false;
- } else if (!name.equals(other.name))
- return false;
- if (sum != other.sum)
- return false;
- return true;
- }
- /**
- * 重写toString()方法
- */
- @Override
- public String toString() {
- return "Students [name=" + name + ", math=" + math + ", chinese="
- + chinese + ", english=" + english + ", sum=" + sum + "]";
- }
- /**
- * 重写compareTo()方法
- */
- @Override
- public int compareTo(Object o) {
- Students s = (Students) o;
- // 总分从高到低
- int num = s.getSum() - this.getSum();
- // 总分相同,按照数学成绩从高到低
- int num2 = num == 0 ? s.getMath() - this.getMath() : num;
- // 总分、数学相同,按照语文成绩从高到低
- int num3 = num2 == 0 ? s.getChinese() - this.getChinese() : num2;
- // 总分、数学、语文相同,按照英语成绩从高到低
- int num4 = num3 == 0 ? s.getEnglish() - this.getEnglish() : num3;
- // 总分、数学、语文、英语相同,按照姓名自然顺序排序
- int num5 = num4 == 0 ? s.getName().compareTo(this.getName()) : num4;
- return num5;
- }
- }
- </font>
复制代码
|
|