This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Extracting from my ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extracting from my ArrayLists" Watch "Extracting from my ArrayLists" New topic
Author

Extracting from my ArrayLists

James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
I have an assignment and it is driving me crazy.

I have managed to create 3 class's that set and can get my values required. I can run my main program which takes values from the user and inputs them into ArrayLists for my class'. However when I come to retrieve and compare the information I get array out of bounds or java.lang.IndexOutOfBoundsException.

I need to have printed. I can post all my code if any one can please help me Thank you

<NAME OF HORSE> has won <amount of races> //finish the below for races won
On the <DATE> at <RaceTrack>
On the <DATE> at <RaceTrack>

int counter = 0;

ListIterator li = HorseRecords.listIterator();
while (li.hasNext())
{
System.out.print("\n"+HorseRecords.get(counter).gethorseName()+" won ");
System.out.println(HorseRecords.get(counter).getRacesWon()+" races");

int[] WinRecordsSize = new int [WinRecords.size()];

for (int i = 0;i < WinRecordsSize.length;i++)
{
ListIterator li2 = WinRecords.listIterator();
System.out.println("\n****PRIINTING WIN RACES ELEMENT ****************\n" +
"Element ["+i+" = "+li2.next().toString());
System.out.println("ON THE "+WinRecords.get(i).getwinDate()+" AT ");
int[] RaceTrackRecordsSize = new int [RaceTracks.size()];

ListIterator li3 = RaceTracks.listIterator();
System.out.println("\n****PRIINTING RACE TRACK ELEMENTS ****************\n" +
"Element ["+i+" = "+li3.next().toString());


System.out.println("\n****GETTING INFORMATION ****************\n" +
"TRACK ID FROM WINRACES ["+WinRecords.get(i).gettrackID()+"\n TRACK ID FROM RACETRACK = "+RaceTracks.get(i).getTrackID());

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Next time you post, please format your code using the Code button above the text area.

You are mixing accessing your array list using an iterator and the get(index) method. You should choose just one - either use an iterator OR use the list.get(...) method. Here is why it is important:

Your code probably looks like this (extrapolating from code you gave):

So you are trying to iterate using the 'li' list iterator, and checking to know if you can iterate any further using li.hasNext(). You retrieve the record out of the list using HorseRecords.get(counter).

The problem is list iterator doesn't know you are using a counter to get the next record. It expects you to use li.next() to get the next record. Since you don't ever call li.next() then li.hasNext() will always be true - you have never moved past the first element. The list iterator should be used by itself to both iterate and get the values from the list:


You are probably doing the same thing with your other iterators. With the bad formatting it is hard to tell.


Steve
James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
Thank you, i think I know where I am going wrong. Only one question (Please note I am new to this forum and Java) when you advised to have an appropriate type as when I add to the HorseRecords list array in there are many data types ints'strings.

I am unsure what you mean for me to declare the currentRecord as the correct type?

(Did not know about the code button will do next time)
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

James Albiston wrote:Thank you, i think I know where I am going wrong. Only one question (Please note I am new to this forum and Java) when you advised to have an appropriate type as when I add to the HorseRecords list array in there are many data types ints'strings.

I am unsure what you mean for me to declare the currentRecord as the correct type?


You have some type of Object that you put into the HorseRecords list. This class has the getHorsesName() and getRacesWon() methods you call. So that is the type you should declare currentRecord as.
James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
Thanks so much it is working - I understand i need to initialise currentRecord as my object DOH!!

Can you tell me what to look up for splitting an int I want to try and figure this on my own (manages to split it to an array before )

Input date : 20080105 (YYYYMMDD)

*****PRINTED OUTPUT *****


05th of January 2008

Thank you so much for your help will be able to sleep now (its working )

Output for 1 record


james won 1 races

On the 2002 at tracking. Thanks for your help!!
James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
HELP PLEASE

How can i go through the list array to achieve what i want going back on forth.
Example Should

PRINT
Record 1 won 2 races
on the <DATE> at track
on the <DATE> at track

Record 2 won 1 race
on the <date> at <track>

// I understand what is wrong it does not look for my previous element and skips[0] and adds to my winrecords and thats why it prints the following.

james won 2 races
On the 09 August 2006 at asdfasd
On the 01 September 2008 at asdfasd
On the 01 June 2001 at asdfasd

sdfasdf won 1 races
On the 09 August 2006 at asdfasd
On the 01 September 2008 at asdfasd
On the 01 June 2001 at asdfasd


I just dont know how i can solve this.

CODE BELOW THANKS ANY ONE WHO CAN HELP I AM GOING NUTS!!// Thing is i understand what i need to do it just how to (hasPrevious only return true and not the object that i would need.






James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
Please is there anyone that could help with this problem ? Or just an idea what i should look up would be great
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Don't know, I am afraid. But you haven't explained the last problem at all well. I don't know what is going wrong.

Why are you re-declaring your HorseRecord and currentWinRecord objects? You appear to have a field and a class called HorseRecord; that is a sure-fire cause of confusion, even if you don't also have a HorseRecords object.
James Albiston
Greenhorn

Joined: Jan 22, 2009
Posts: 6
Thank you for your reply.

I have declared my HorseRecord (class) and created HorseRecords (Going through a loop until the user exits) inside this calls my WinRecord class adding WinRecords (which should match an id field I have created)

When I go through the while (li.hasNext() loop this prints the current record(s) which seems to work.

When I then try a call to get my WinRecords to search HorseRecord.recordID to match in the WinRecords array and then print ONLY the ones that match the record. ( Hope I am making sense)

I can then have
Record 1 won 2 races // here inside record 1 it should only print the 2 races where it has won.
<???>
>???
Record 2 won 1 race // here it should only return the WinRecords that match and only return 1 entry
<ONE ONLY>

It seems to get all in the array which is correct because I cant go out of the while loop as I need to print all records - i need to validate and then print the resultant HorseRecord.recordID == WinRecord.recordID (WinRecord.recordID can have more than on collection to encapsulate the records

Any help would be great or advice I feel so stupid I understand it just cant figure out how to do it all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
So you have got a horse in one List, and wins in the other List, and you are trying to match the horse in one List with its wins in the other List? Is that it? I am still finding the problem hard to understand.

Have you been told to use ArrayLists? Go through the Java Tutorials about Collections, and you find an interface where you can match one value against another. You can insert a Horse, with a win associated, or even a List of wins.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting from my ArrayLists
 
Similar Threads
Explanation for Code : Parsing XML using Java
Java help.
Explanation for Code
Someone up for a challenge?
using array list