3

I'm trying to define an array starting from data that come from another array. The code will explain the situation better than thousands words.

public class QualityCheck {



public QualityCheck (JTable table)
{
    //the data come from a JTable (that represents a school timetable)
    String [] dailyLessons= new String[table.getColumnCount()];
    String [] dailyClasses= new String[table.getColumnCount()];

    //checking all the days
    for (int i=1; i<table.getColumnCount(); i++)
    {
        //checking all the hours in a day
        for (int j=0; j<table.getRowCount(); j++)
        {
            //lesson is an array that contains the subject and the room in which the subject is erogated
            //lesson[0] contains the subject
            //lesson[1] contains the room
            String[] lesson = ((TabellaOrario.MyTableModel)table.getModel()).getLesson(j,i);

            //I'd like to put ALL the daily subjects in dailyLesson
            dailyLessons[j] = lesson[0];

            //I'd like to put All the daily rooms in dailyClasses
            dailyClasses[j] = lesson[1];

        }

        //trying if dailyLessons has the elements
        for (String s: dailyLessons)
        {
            System.out.println(s);
        }

    }   
}
}

If a run this code, the compiler protest with this error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 7

and it evidence the string

dailyLessons[j] = lesson[0];

How can I do to define dailyLesson?

3
  • +1 for this "The code will explain the situation better than thousands words." Commented Sep 15, 2013 at 15:07
  • What does table.getColumnCount() return? (Or what is the length of dailyLessons?) Commented Sep 15, 2013 at 15:11
  • table.getColumbCount() return the number of column in a table. dailyLessons is an array with all the subjects that are erogated in a single day (represented by a column) Commented Sep 15, 2013 at 15:50

2 Answers 2

1

You are allocating both of the arrays to the same size table.getColumnCount(), and then you use index j for both of them again, which goes up to table.getRowCount() - 1.

You probably should allocate one of them to size table.getRowCount(), and then use j as the index for only that one, and i for the other, but you never use dailyClasses so I'm not sure.

Edit: Apparently the intent is to fill both the arrays with the data of one column. Then the fix is to change the size of the arrays to the amount of rows:

// Changed table.getColumnCount() -> table.getRowCount()
String [] dailyLessons= new String[table.getRowCount()];
String [] dailyClasses= new String[table.getRowCount()];
Sign up to request clarification or add additional context in comments.

3 Comments

Hi kiheru and thanks for trying to help me. I can use j for both array, because I want to define the same location for each array. But, I repeat, I don't know why it does not work
Ok. I think I understand now what you are trying to do. I'll edit the answer a bit
Kiheru, I have resolved. I was wrong to define the array. The right one is: String [] dailyLessons= new String[table.getRowCount()]; String [] dailyClasses= new String[table.getRowCount()]; Thank you!
1

You init the arrays with table.getColumnCount() and loop using j < table.getRowCount().

If table.getColumnCount() is smaller that table.getRowCount() then you will get AIOBE.

You need at least to init the arrays with table.getRowCount().

EDIT

You could create a little class with encapsulates dailyLessons and dailyClasses

class Lesson {
    public String dailyLesson;
    public String dailyClass;
}

and create an array of that class, this way you'll always have the same number of daily lessons and classes:

String [] lessons = new Lesson [table.getRowCount()];

and later inside the loop:

lessons.dailyLesson = lesson[0];
lessons.dailyClass = lesson[1];

Also you could use an ArrayList instead of a simple array so you wont have to bother about the size of the array.

2 Comments

You're right, I will provide. Thank you. I would have appreciated a snippet to understand what were you saying
@Bernheart, glad i could help. I guess the snipet you were looking for was provided by kiheru. I have little addition, see my edit.

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.