I'm working on a simple program for school that's a simple String builder, I'm properly defining the array size, yet when I run the program, ArrayIndexOutOfBoundsException: 0 is thrown. I initialize the variables in the constructor, name and classes. name is the student's name, classes is how many classes the student has each day. The size of my arrays is defined using classes. Here is my constructor and variables.
protected String name = "";
protected int classes;
private String schedule = "";
private String[] course = new String[classes];
private String[] room = new String[classes];
private int[] Period = new int[classes];
public StringBuilderHandler(String name, int classes) {
this.name = name;
this.classes = classes;
}
I'm using a for loop to set what String schedule will be.
private void setClass(int index) {
Scanner scan = new Scanner(System.in);
class[index] = scan.nextLine();
}
private void setPeriod(int index) {
Scanner scan = new Scanner(System.in);
period[index] = scan.nextInt();
}
public void setRoom(int index) {
Scanner scan = new Scanner(System.in);
room[index] = scan.nextLine();
}
public void buildSchedule() {
for (int i = 0; i < classes; i++) {
System.out.println("What is your class?");
setClass(i);
System.out.println("What period is this class?");
setPeriod(i);
System.out.println("What room is this class?");
setRoom(i);
schedule = schedule +"Period "+period[i]+"\t"+course[i]+"\tRoom "+room[i]+"\n";
}
}
Any ideas?
class[index]becourse[index]insetClass?