aspose file tools*
The moose likes Java in General and the fly likes Searching Collection 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 » Java in General
Bookmark "Searching Collection" Watch "Searching Collection" New topic
Author

Searching Collection

Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
Hi all,
I am having trouble figuring out how to search a collection. The collection holds three strings (name, address, telephonenum) i would like to be able to search using a name as the search criteria.



Branch class that recieves input from estateGUI.


CustomerCollection class which builds the collection and has the method for add/find client etc:


Does anyone have any ideas on how i can find a specific client in a collection.

Thanks
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Start by fixing addClient().First off, index is initialized to 0 and collection to a 5-element array, so the if test will fail and no Client will be added to the array. As index is incremented each time, the first five calls will result in no Clients. The sixth call will result in an ArrayIndexOutOfBoundsException since index (5) is outside the limits of the array (0..4).

Now, to find a particular Client, you need to iterate through the Clients in the array until you find one with the same name as that passed into findClient(). Then return that found Client or null if none match. Make sure to use the String.equals() method to compare Strings rather than ==.
Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
Hi,

Great help thankyou...

I have sorted my add client method out which is much more logical now..

Many thanks

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You're welcome. Keep 'em coming!
Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
Sorry to bother you again...

I have a question...

I have slightly changed the addClient method now so that the user cannot store more than 5 users, although when testing my GUI out i can add as many clients as i like without any error message what so ever.

Here's my addClient method:



Thanks again..

[ February 23, 2005: Message edited by: Rich Charlton ]
[ February 23, 2005: Message edited by: Rich Charlton ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rich Charlton:
I have slightly changed the addClient method now so that the user cannot store more than 5 users, although when testing my GUI out i can add as many clients as i like without any error message what so ever.
Valid array index values go from 0 to the length minus one (0 to 4 in this case). You are causing a NullPointerException which Swing is swallowing for you.
Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
Hi, sorry about this cant seem to get the logic right..
Firstly my GUI gets the text name input

which passes the values name, address, telephonenum over to branch class by calling the method add()

The add() calls the method addClient() from customercollection class below


Output is my system.out.println("Error"); obviously my my values are wrong somewhere and the statement if (index == collection.length) has got to be incorrect.

Im not sure why this is not working.
To clarify :[code]
private int arraySize = 5; //this is how many elements i would like to have in the array
public int index = 0; //this is an index for each element

If you have any more help, that would be great

Thanks again..

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The "if(index == collection.length)" is correct, but it doesn't do what you want I don't think. The index instance variable keeps track of how many Clients are stored in the collection. It starts at 0. The expression collection.length is the number of elements that the array can hold -- not how many are filled. Since you create the array with 5 elements, it evaluates to 5.

In Java (and most languages) 0 does not equal 5, and your if test fails and falls into the else block.

Looking at your first two code blocks I see something that you probably don't want to do. In the first, you create a new branch and add the client information to it. In that new branch, a new empty CustomerCollection is created and you add the information to it.

Thus, every time you hit the button that fires off the event, you are adding a new client to a new collection. Since addclientActionPerformed() doesn't save the new branch (it puts it in a local variable which disappears when the method ends) in a non-temporary variable, you throw out all the objects you've just created.
Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
Hi,

i have removed making a new branch every time i click the find button on my GUI, and thats seems to of helped.

Heres what i have now..

Only problem now is that when i do click find it just returns whatever i type into the textfield. It is not searching the array for stored items.

Any ideas....
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rich Charlton:
Be careful with your array bounds in the if test. Arrays have index values from 0 to length-1. If index == length (passes test) you will try to access element #5 (the sixth element) of the array, resulting in an exception.

You might want to rename the "index" variable as it's a really confusing name. You use it as an index into an array, sure, but then you may as well call it "integer" since it's an int. I say this because you're using it as an index stepping value in your for loop.

To find a particular element in the array, you want to check every valid element from 0 to total-1 -- not length-1 here because you may only have 2 elements in a 5 element array, and you can't check null elements without another exception.

If you were doing this outside of Swing, you'd be seeing these errors on your console. In any case, that's all that jumps out at me right now.
Rich Charlton
Greenhorn

Joined: Nov 11, 2004
Posts: 19
right i think i have sorted my addClient method now:


Output after entering 5 clients:


The above is what i want which is cool..

Now to my findClient method....This works to a point...It will find a specific name...however when i enter a name not stored i get null exception error then my "Error Client not Found" message. How can i avoid this..or have you allready explained this to me...ive probably got my loop slightly wrong again...for the 400th time...

thanks
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
No worries, this will all become old hat to you after a while. You're very close with your find loop, but you're missing an important piece which causes the NullPointerException. Here's what the array looks like once you've added 3 clients:Since collection.length is always equal to 5 -- the number of elements it can hold -- you need to find a different stop condition for your loop.What variable could tell you where to stop? Hint: an instance variable already holds the correct value, so simply replace "collection.length" with that variable name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching Collection
 
Similar Threads
NOT Abstract ERROR
Pressing a button to open a new window
Applet won't work in web page!
simple login - help asp
How to create hot key?