• Post Reply Bookmark Topic Watch Topic
  • New Topic

Index out of bound Exception?  RSS feed

 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again

So I am having a teeny weeny issue with the following method where it keeps on giving me index out of bounds exception at line 9. What I am trying to do here is use an array containing dates(datesOfFirstWeekClasses) and its size (sessionsPerWeek) to get a list of all dates in the term.

What I am expecting from the method is to get for me the dates of all the classes in a term by using the dates of the days in the first week of the term.

"datesOfFirstWeekClasses" is an array of type Date. During testing, it only had one date.
"sessionsPerWeek" is the size of the array "datesOfFirstWeekClasses".
"Calender" that is a class I created, not the java one.
"convertDateToCalendarForm" is a method I created to convert Date to Calendar(java's Calendar).


 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming it's this line with the error:
Put in some System.out.println(...) statements to check what sessionsPerWeek and i are.

It's also good to post the error message.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Calender" that is a class I created, not the java one.
"convertDateToCalendarForm" is a method I created to convert Date to Calendar(java's Calendar). 

Super confusing.  Consider renaming your Calendar class.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better way to write this is The idea is to use the interface as the type, not the implementation.  It gives your code more flexibility.
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
"Calender" that is a class I created, not the java one.
"convertDateToCalendarForm" is a method I created to convert Date to Calendar(java's Calendar). 

Super confusing.  Consider renaming your Calendar class.


it is but my course project requires me to use that name, so I misspelt it to avoid confusing myself lol :P Sorry I confused you.

I did what you said and the loop sets prints i = 1 and sessions also equals 1 then gives me the error as follows
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the inner for-loop, you don't use k and you increment i.  That doesn't make sense to me.  What are you trying to accomplish here?
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
In the inner for-loop, you don't use k and you increment i.  That doesn't make sense to me.  What are you trying to accomplish here?


K represents the size of the array "datesOfFirstWeekClasses" aka "sessionsPerWeek". I don't need to use it inside the loop but i need to increment it every time, 'i' represents the element number in the array list "days".

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through your code with pencil and paper, writing down what the values of i and k are after each run of the loops. Imagine you have a small List, maybe 4 elements. I think you will find you are increasing the numbers too much.
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go through your code with pencil and paper, writing down what the values of I and k are after each run of the loops. Imagine you have a small List, maybe 4 elements. I think you will find you are increasing the numbers too much.


FIXED IT

The pen and paper technique worked mate.  I took so long to post this because I had a logical error in a different class that caused the method "calcTotalSessions" to return wrong values :P

But it is great now. You are the best.

Cheers



 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's great!  And thanks for posting the solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!