wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes int[] find(String[] criteria) -- help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "int[] find(String[] criteria) -- help please" Watch "int[] find(String[] criteria) -- help please" New topic
Author

int[] find(String[] criteria) -- help please

Derick Potgieter
Greenhorn

Joined: Feb 19, 2004
Posts: 13
Hello,

I`m struggling with the find method, i have my for loop ready to search thro the database and compare each criteria field. But how do i actually
compare a field if say criteria[i] = For example, "Fred" to return "Fred" or "Freddy".

How do you match the first 4 letters then to all the search criteria??

Thanks
Derick
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Derick,

Originally posted by Derickll Potgieterll:
how do i actually
compare a field if say criteria[i] = For example, "Fred" to return "Fred" or "Freddy".

How do you match the first 4 letters then to all the search criteria??


Have a look at the startsWith(String str) method in String.

Cheers,

Jon


SCJD, SCEA
Derick Potgieter
Greenhorn

Joined: Feb 19, 2004
Posts: 13
Thanks, sort of stupid of me...

i`m getting the criteria searching part right now, but i still need to make a list and then get a final list, please can you check this code and let me know if there is a better solution or how i can make a single list??


but how can i track my record numbers, i can match the data.
in the 2 lists and then make a final list, but i`m not
tracking my records numbers??
Jon Entwistle
Ranch Hand

Joined: Feb 20, 2003
Posts: 118
Hi Derick,

The return from this method is just an array of record numbers - to do this you need to iterate over all of your records, incrementing a counter variable on each iteration. If the record matches (and the record has not been deleted), simply add an Integer object for the value of the counter to a Collection. When finished, declare a new int[] to the size of this Collection, populate it by converting the Integer values in your collection then return this array.

I have to be honest, I had trouble following your code. I would certainly avoid non-idiom constructs such as incrementing a different variable to your control variable within the for loop:



these make the code very dificult to follow.

Hope this helps

Cheers,

Jon
[ May 31, 2004: Message edited by: Jon Entwistle ]
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Derickll,
I reckon your code wouldn't work for all the cases.
If the criteria array contains 2 fields and if they both match, then it is added twice in the arraylist. Also if it contains only 1 field and the others are null, then it is added multiple times into the arraylist. Isn't it?
I guess variable "x" in your inner for loop could be your record number.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: int[] find(String[] criteria) -- help please