0

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;
}


}
3
  • where I got this mySchedule[i][j]="Test"; Commented Mar 3, 2014 at 4:41
  • System.out.println(mySchedule); should read System.out.println(java.util.Arrays.toString(mySchedule[i])); Commented Mar 3, 2014 at 4:43
  • You should read this article and especially this i.e. I mean shorten your code. Commented Mar 3, 2014 at 4:46

1 Answer 1

4
for (int j=0; j< employeelist.size(); i++) 
{
   mySchedule[i][j]="Test";
}

Are you supposed to increment i here?

Sign up to request clarification or add additional context in comments.

1 Comment

You are very detail oriented @Kuma. Thanks for spotting this so quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.