student[nStudent] = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3])); This line cause "java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1".
How can I solve this problem?
public Student(String name, int physic, int chemistry, int math) {
/*this.name = name;
this.physic = physic;
this.chemistry = chemistry;
this.math = math;*/
setName(name);
setPhysic(physic);
setChemistry(chemistry);
setMath(math);
getName();
getPhysic();
getChemistry();
getMath();
int sum = this.physic + this.chemistry + this.math;
this.ave = sum / 3;
}
private static Student[] readData(String filename){
int nStudent = 0;
Student[] student = new Student[21];
String string = null;
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((string = bufferedReader.readLine()) != null){
String[] arr = string.split(" ");
student[nStudent] = new Student(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));
nStudent++;
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
return student;
}