Reputation: 91
I have a class schedule with a method createSchedule that is supposed to create a schedule for the whole week. I am using a nested for loop to do the job but I am having an error of this type
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at com.sib.TManager.Schedule.createSchedule(Schedule.java:58)
at com.sib.TManager.App.main(App.java:32)
here is my code and I can't figure out what is wrong with it. Can someone help please?
public class Schedule{
private String[][] scheduleList;
private final String[] DAYS={"Monday","Tuesday","Wednesday","Thursday","Friday"};
public void createSchedule(){
Random rdm= new Random();
ArrayList<String> workstationlist = new ArrayList<String>();
ArrayList<String> employeelist = new ArrayList<String>();
Scanner s;
//load employees into a list
try {
s = new Scanner(new File("EmployeeList.txt"));
while (s.hasNext()){
employeelist.add(s.next());
}
s.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//load workstations into a list
try {
s = new Scanner(new File("WorkStationList.txt"));
while (s.hasNext()){
workstationlist.add(s.next());
}
s.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
System.out.println("List of employees");
for (Iterator iterator = employeelist.iterator(); iterator.hasNext();) {
System.out.println(iterator.next().toString());
}
System.out.println();
System.out.println("List of workstations");
for (Iterator iterator = workstationlist.iterator(); iterator.hasNext();) {
System.out.println(iterator.next().toString());
}
String[][] mySchedule = new String[DAYS.length][employeelist.size()];
//creation of the schedule for the week
for (int i = 0; i < DAYS.length; i++) {
for (int j=0; j< employeelist.size(); i++)
{
mySchedule[i][j]="Test";
}
}
//printing schedule in console
for (int i = 0; i < DAYS.length; i++) {
for (int j=0; j< employeelist.size(); j++)
{
System.out.println(mySchedule[i][j]);
}
}
}
}
public String[][] getScheduleList() {
return scheduleList;
}
public void setScheduleList(String[][] scheduleList) {
this.scheduleList = scheduleList;
}
}
Upvotes: 0
Views: 221
Reputation: 5314
for (int j=0; j< employeelist.size(); i++)
{
mySchedule[i][j]="Test";
}
Are you supposed to increment i
here?
Upvotes: 4