aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Urly bird find method: are search criteria Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Urly bird find method: are search criteria "and"- or "or"-composed?" Watch "Urly bird find method: are search criteria "and"- or "or"-composed?" New topic
Author

Urly bird find method: are search criteria "and"- or "or"-composed?

Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hi all,

having written the find() method in Urly Bird some days ago, I just now only realized that I'm not clear about whether a record matches an array of criteria when it matches one criterium (in one field, OR version) or when it matches all of the given criteria (in all specified fields, AND version).

This is the text from Sun:

Returns an array of record numbers that match the specified criteria. Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)

Please help - how do you understand this requirement?

cheers
Sigrid
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Howdy.

Here's what I did: for each record in the database, I verify if each field match the value present in its corresponding position in the String array. So if the value of only one field starts with its corresponding value in the String array, I add this record to the search results. For instance:



In the example above, the record would be included in the list of results, since the name value starts with the value of the corresponding position in the String array. If only one field fits this rule, then it is enough for this record to be included in the list of results.

Also, in my case, I did not care about the case of the values.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hi Bob,

thanks for your reply. In the meantime, I think I tend to the "have to match all criteria" interpretation, but it seems it's really ambiguous and so probably both should work (I should add it to choices.txt I suppose

ciao
Sigrid
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Note that this search is not the result that will be displayed to the user. This is how you have to implement your find method, but the results that are displayed to the user have to be filtered, according to the selected options in the GUI. So if the user wants to search for Hotel Name And Location, you'll have to analyze the results returned from your find method and see which of them match the criteria specified by the user.
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hi Bob,

I do not understand why you have these 2 steps - first return all, then filter in the GUI... Do you think this is required - or could I just do as I am planning and filter in the search itself, by "AND"-composing the criteria?

cheers,
Sigrid
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

You have to do this because the data you get in the find method (the String array) is not able to tell whether the user chose an "and" or an "or" search. Also, following the comments of the find method:



So, if a particular record has as hotel name "SCJD Forum", and the value in the corresponding position in the String array is "SCJD", then this record will be included in the list of results. This implements the code abstracted in the comments of the find method. But you have to filter the results. First, because of the mode of the search (can be either "and" or "or, and you're not able to tell this just by the String array), and also the returned results does not guarantee that what was returned is what the user is searching. Still using the example above, the user enters "SCJD" in the hotel name field (let's suppose the mode of the search is "and", not mattering the hotel location entered by the user), and there's one record with a hotel name called "SCJD Forum". So this record is returned, but practically this record shouldn't be displayed to the user. In other words, for the find method, you have to use the startsWith method; to filter the results that are displayed to the user (applying correctly the "and" or "or" search modes), you have to use the equals method. To finalize, in the example above, if the user provided "SCJD Forum", and the mode was hotel name OR hotel location, then this record would definitely be displayed.
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hi Bob,

thanks a lot for your patience. I realize I overlooked a part of the specification, about the GUI:

It must allow the user to search the data for all records, or for records
where the name and/or location fields exactly match values specified by
the user.


Thanks again, I would either have missed it or noticed it only when I start the GUI...
Kris Krason
Greenhorn

Joined: Aug 05, 2005
Posts: 25
Well,
AFAIR there is nothing in the specification that we should allow "or" search.
So there is no need of filtering. And I think that or search with those fields that we have, doesn't make sense (city "Dallas" or hotel name "Hilton" ?)


Kris Krason<br />SCJP1.4 (96%)<br />SCJP1.5 (91%)<br /> <br />See my <a href="http://krisreviews.com/2006/12/code-complete-second-edition/" target="_blank" rel="nofollow">Code Complete review</a><br /> <br /><a href="http://www.coderookie.com/2006/tutorial/the-pseudocode-programming-process/" target="_blank" rel="nofollow">Pseudocode Programming Process</a>
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

So there is no need of filtering.


Not it the find method, as I had said previously. This is to be done in the GUI.

And I think that or search with those fields that we have, doesn't make sense


This is what I have in my requirements, in the "The User Interface" section:


It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
I'm of Kris's mind too.

must allow to search for records where the name or location fields exactly match values specified by the user


does not mean, the GUI must provide OR-search. I understand this statement so, that the GUI beside the AND-search must also allow to search by name or by location separately.

Is there anybody, who did not provide any OR-search and passed?
I'm not realy up for remaking my program
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

I don't think there is any doubt on this subject: the find-method in your Data-class returns a record if the record has at least one field-value that starts with the given search value for that field. so if 2 or 3 or 4 fields start with the corresponding criteria it doesn't matter: if 1 field-value starts with the given search value for that field its record number should be returned.

Your gui expects only exact matches and you are able to search on:
- hotelName
- location
- hotelName and location

so you have to do some filtering in your gui because not all records that are returned (in the case of a search on "hotelName and location") match both criteria.

A complete similar question is asked in another thread, where i gave a small example of how your application should behave. you can find it here


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

... and that's my good buddy Roel!
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
Thanks a lot. I read all four topics but still do not understand why this


means this

your Data-class returns a record if the record has at least one field-value


In my opinion a record mathces the specified criteria only if all criteria and fields matche. Otherwise if there is at least one null value in criteria, this would mean, that any record matches this creteria, because at least one condition is already true.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Maybe I can assist a bit. The requirement says in the User Interface section:
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.

Now in the interface
Returns an array of record numbers that match the specified criteria. Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)


Yet somewhat contradicting but stick to the requirement, which means:
1) search for ALL records
2) search for name AND/OR location fields EXACTLY MATCH...

Point 2 definitely contradict with the example given in the interface description. The AND/OR part. To satisfy OR, what is needed. Name only OR location only right? Now AND part - this allows users to select or type in both name and location. But wait what happens if they select AND and only input EITHER name OR location.... this makes it OR again. Another way of looking is for AND, you can require the user to enter both name and location to make ensure both name and location are NOT NULL.

Hope this helps


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
K. Tsang wrote:Maybe I can assist a bit. The requirement says in the User Interface section:
It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.

Now in the interface
Returns an array of record numbers that match the specified criteria. Field n in the database file is described by criteria[n]. A null value in criteria[n] matches any field value. A non-null value in criteria[n] matches any field value that begins with criteria[n]. (For example, "Fred" matches "Fred" or "Freddy".)


Yet somewhat contradicting but stick to the requirement, which means:
1) search for ALL records
2) search for name AND/OR location fields EXACTLY MATCH...

Point 2 definitely contradict with the example given in the interface description. The AND/OR part. To satisfy OR, what is needed. Name only OR location only right? Now AND part - this allows users to select or type in both name and location. But wait what happens if they select AND and only input EITHER name OR location.... this makes it OR again. Another way of looking is for AND, you can require the user to enter both name and location to make ensure both name and location are NOT NULL.

Hope this helps


What you want to say? Sorry, I didn't understand.
Let's different the questions:

1. The method find in the interface:
a) A record mathes a criteria if all non-null criteria match corresponding record fields.
b) A record mathes a criteria if at least one non-null criterion matches corresponding record field.

2. The search in the GUI:
Beside the ability to search for all reacords and for records where both criteria match
a) The GUI must allow the user to search for records where the name field or the location field exactly match values specified by the user.
b) The GUI must allow the user to search for records where only the name field exactly matches a value specified by user and for records where only the location field exactly matches a value specified by user.

For people who passed: how did you implement your program?
For people who pending: what is your opinion?

I (pending) rather lean to:
1 - a
2 - b
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

OK let me rephrase what I want to say. From the requirements there can be 3 possibles cases for find:
1) all records (all null fields in the array)
2) name or location only (only one non-null field in array)
3) name and location (2 or more non-null fields in array)

Now to cater for exactly match vs start with. In my GUI I used combo boxes to present the names/locations. This guarantees exact match right? Now in my find method I used "startWith" to produce the results which satisfy "Fred" matches "Freddy" or "Fred" right?

Some people uses text fields in their GUI which can be confusing. For example you have a set of radio buttons in the search UI saying name only, location only, or name and location. Now if you select name and location radio button, the name and location text fields can enter text. This is what I was mentioning:
In "name and location" option, if you JUST enter name and leave location blank ... what does this give you - name only right? So in your find method you would need to consider this too - a way to determine how many non-null fields.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

K. Tsang wrote:1) all records (all null fields in the array)


Howdy, K!
Partner, just one question, if I call your find() method, passing a String array, only with null values, will I get all records?
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
K. Tsang wrote:OK let me rephrase what I want to say. From the requirements there can be 3 possibles cases for find:
1) all records (all null fields in the array)
2) name or location only (only one non-null field in array)
3) name and location (2 or more non-null fields in array)


This is exactly how I implement my program.

K. Tsang wrote:
Now to cater for exactly match vs start with. In my GUI I used combo boxes to present the names/locations. This guarantees exact match right? Now in my find method I used "startWith" to produce the results which satisfy "Fred" matches "Freddy" or "Fred" right?


I just set all remaining characters of criterion with blanks. This also guarantees exact matching, because all record fields have a fix length and not used characters are blanks.

K. Tsang wrote:
Some people uses text fields in their GUI which can be confusing. For example you have a set of radio buttons in the search UI saying name only, location only, or name and location. Now if you select name and location radio button, the name and location text fields can enter text. This is what I was mentioning:
In "name and location" option, if you JUST enter name and leave location blank ... what does this give you - name only right? So in your find method you would need to consider this too - a way to determine how many non-null fields.


I use text fields too. Do you use comboboxes? But when do you refresh them? Do they not always contain names and locations of previous search?

Why I need radio buttons? In my GUI if the user want to search only by one criterion, he must just let other criterion blank. Do you see any troubles in this solution?

Thank you for you answers.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
Roberto Perillo wrote:
K. Tsang wrote:1) all records (all null fields in the array)


Howdy, K!
Partner, just one question, if I call your find() method, passing a String array, only with null values, will I get all records?


My find() method would also return all records in this case. Do you see any troubles, Roberto?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Yup that's how I interpret this: "A null value in criteria[n] matches any field value":

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Sergiy Kononenko wrote:I use text fields too. Do you use comboboxes? But when do you refresh them? Do they not always contain names and locations of previous search?

Why I need radio buttons? In my GUI if the user want to search only by one criterion, he must just let other criterion blank. Do you see any troubles in this solution?


I used combo boxes. My boxes don't contain the previous search items. If you do, it is really a Swing problem (where you instantiate the window).

My GUI combo boxes are disabled by default until the user clicks one of the radio buttons and activate the appropriate combo box. The default item is "ALL" meaning null for the find method array.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Sergiy Kononenko wrote:Do you see any troubles, Roberto?


Well, I implemented it differently; if I pass a String array with only null values, then no records will be retrieved. To retrieve all records, I added a method called getAllRecords in the interface I created and that extends the interface provided by Sun. But I guess this is also a matter of choice, so there should be no problems.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
K. Tsang wrote:
Sergiy Kononenko wrote:I use text fields too. Do you use comboboxes? But when do you refresh them? Do they not always contain names and locations of previous search?

Why I need radio buttons? In my GUI if the user want to search only by one criterion, he must just let other criterion blank. Do you see any troubles in this solution?


I used combo boxes. My boxes don't contain the previous search items. If you do, it is really a Swing problem (where you instantiate the window).

My GUI combo boxes are disabled by default until the user clicks one of the radio buttons and activate the appropriate combo box. The default item is "ALL" meaning null for the find method array.


I mean when do you fill your comboboxes?
Let's imagine, the user knows that a new hotel accomodation was added into the database. How does he get this accomodation displayed in the GUI? Do he have to perform a search two times?

Do you see any troubles in my solution with only two text fields? Does it fulfil all requirements?
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Load the combo boxes whenever the search window opens so that any new/removed names/location will be reflected.

Text fields sure do meet the requirements. But do look out for capitalization in the first letter.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
Roberto Perillo wrote:
Sergiy Kononenko wrote:Do you see any troubles, Roberto?


Well, I implemented it differently; if I pass a String array with only null values, then no records will be retrieved. To retrieve all records, I added a method called getAllRecords in the interface I created and that extends the interface provided by Sun. But I guess this is also a matter of choice, so there should be no problems.


I think this is because we are different opinion about whether the matching conditions must be associated by OR or by AND.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
K. Tsang wrote:But do look out for capitalization in the first letter.


If I do it, this would contradict the requirements. The value must exactly match the specified criterion.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2517
    
    8

Sergiy Kononenko wrote:
K. Tsang wrote:But do look out for capitalization in the first letter.


If I do it, this would contradict the requirements. The value must exactly match the specified criterion.


That's why I didn't use text fields. For example in the database file the location may be "Smallville". Now you type "smallville" ... the find method will not find records because the first letter does not match. Of course you can always cater this somewhere. And I doubt this is an easier task depends on what the value is.
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
K. Tsang wrote:
Sergiy Kononenko wrote:
K. Tsang wrote:But do look out for capitalization in the first letter.


If I do it, this would contradict the requirements. The value must exactly match the specified criterion.


That's why I didn't use text fields. For example in the database file the location may be "Smallville". Now you type "smallville" ... the find method will not find records because the first letter does not match. Of course you can always cater this somewhere. And I doubt this is an easier task depends on what the value is.


I don't realy think my GUI will be ever used by a real user.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

the main reason for using a combo for your search criteria is making your app more user friendly because user can't make a typo (e.g. capital letters). But the big problem is, like already indicated by someone: when are you going to refresh those combo's?

In my opinion there are 2 valid solutions:
- an extra menu-item "Refresh search criteria" which refreshes all search criteria (so both combos)
- a button after each combo to refresh the corresponding combo

And of course some input fields instead of combo's certainly fulfill requirements. you could even enhance ease of use by performing a search whenever the enter key is pressed when one of text fields has focus.

Roberto Perillo wrote:
K. Tsang wrote:1) all records (all null fields in the array)

Howdy, K!
Partner, just one question, if I call your find() method, passing a String array, only with null values, will I get all records?

You certainly will get all records and i mentioned that in the javadoc of this method. but this whole discussion makes me doubt about the consistency of my implementation
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2266
    
    3

Roel De Nijs wrote:...but this whole discussion makes me doubt about the consistency of my implementation


No worries, champ! I believe this may also be a valid choice. This is a point of the certification that is supposed to put doubts in your head. As long as you identify and take decisions on these points, it should be ok. It's just that, my understanding of this method was that "A null value in criteria[n] matches any field value" as long as "A non-null value in criteria[n] matches any field value that begins with criteria[n]", because if any null value in criteria[n] matches any field value and we're only using the name and location fields, the other fields would always be null and all records would always be retrieved. However, if you retrieve all records only when all fields are null, then I think it may be a valid choice too.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Hi Guys,

Man this is a hard forum to moderate! It's always tough to determine exactly when a discussion is getting a little too detailed. That said (and acknowledging that I might be being arbitrary), this one's feeling a little uncomfortable to me... let's back off a little!

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
Dears;


2. The search in the GUI:
Beside the ability to search for all reacords and for records where both criteria match
a) The GUI must allow the user to search for records where the name field or the location field exactly match values specified by the user.
b) The GUI must allow the user to search f


hi Sergiy Kononenko iam with you. i will upload my assignment next week and i think after 5 week you can see if i pass or failed .

because:


also i will not write any think in my choices.txt file regarding this issue because it a requirement. i think any one alter this behavior must write in choices.txt about his thought so the examiner will understand his thought. may be or may be i am wrong and i will fail after 5 weeks.
what do you think ?

Best regards.
Mohamed Sulibi
SCJP, SCJD in progress ... (from 1/8/2007 till now but may be after 5 weeks)
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
mohamed sulibi wrote:
also i will not write any think in my choices.txt file regarding this issue because it a requirement. i think any one alter this behavior must write in choices.txt about his thought so the examiner will understand his thought. may be or may be i am wrong and i will fail after 5 weeks.
what do you think ?


Dont worry, I let you know if you are wrong before. ;)
I'm going to send my program this week.
mohamed sulibi
Ranch Hand

Joined: Sep 04, 2005
Posts: 169
dears;


Dont worry, I let you know if you are wrong before. ;)


thank you ... so i will give you half of the re-submission fee.

please did you write in you choices.txt about this interpretations or thought ? because i think it is not needed to state that "i consider this javadoc mean ... ".

Best regards.
Mohamed Sulibi
SCJP, SCJD in progress (From 1/8/2007 till now)
Sergiy Kononenko
Greenhorn

Joined: Mar 20, 2009
Posts: 23
mohamed sulibi wrote:dears;


Dont worry, I let you know if you are wrong before. ;)


thank you ... so i will give you half of the re-submission fee.

please did you write in you choices.txt about this interpretations or thought ? because i think it is not needed to state that "i consider this javadoc mean ... ".

Best regards.
Mohamed Sulibi
SCJP, SCJD in progress (From 1/8/2007 till now)


Yes, I did.
I think it is better to write too more, then too les ;)
Furthermore I'm going to use this document when I will prepare for exam. I want that all my decisions was described in a one document.
Olu Shiyan
Ranch Hand

Joined: Jun 10, 2010
Posts: 57

Just stumbling on this discussion and a host of others on the implementation of the 'find' method.

--My 'find(String[] criteria)' method will return all records if all elements of the String array 'criteria' are null.

--It'll return a record if any field in that record starts with the corresponding field in criteria array

---else it simply moves on to the next record.


Correct guys?


Thanks




SCJP 6, OCMJD6
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5407
    
  13

Hi Olu,

I have another implementation:
  • return all records if all criteria are null
  • if not all criteria are null, the null criteria are ignored and the others are used to search for records: a record is a match if at least one field of the record starts with the corresponding (not-null) criterium (in a case-insensitive way)

  • But of course that's not the only valid implementation.

    Kind regards,
    Roel
    Olu Shiyan
    Ranch Hand

    Joined: Jun 10, 2010
    Posts: 57

    Cheers Roel. I didn't describe it well. My find method does just that.


    Thanks
    Roland Kozma
    Greenhorn

    Joined: Nov 27, 2013
    Posts: 4
    Roel De Nijs wrote:Hi Olu,

    I have another implementation:
  • return all records if all criteria are null
  • if not all criteria are null, the null criteria are ignored and the others are used to search for records: a record is a match if at least one field of the record starts with the corresponding (not-null) criterium (in a case-insensitive way)

  • But of course that's not the only valid implementation.

    Kind regards,
    Roel



    Hi Roel and everybody from here!

    You said "the null criteria are ignored". -> Why do you ignore the null criteria?

    If you consider that "A non-null value in criteria[n] matches any field value that begins with criteria[n]." means that
    any record that match at least one criteria should be returned,
    why don't you interpret the sentence "A null value in criteria[n] matches any field value." in the same way:
    If at least one null is present in the criteria, all the records from the database should be returned (because a null match any field).

    Because these seams contradictory I think that returning just the records which match all the criteria (using AND) should be a valid interpretation too.
    Have you heard that somebody failed because of this interpretation?
    Until I stumbled in this subject on the forum I had no doubt that we should use AND to match all the provided criteria.

    Regards,
    Roland
    Paweł Baczyński
    Bartender

    Joined: Apr 18, 2013
    Posts: 1020
        
      16

    That's what I am doing.
    I implemented find() using AND approach. A record is added to results when and only when all fields matches the criteria.
    As find() method in Sun's interface matches using startsWith() and GUI should display only records that exactly matches, I extended Sun's interface and added method findExact().
    So I don't need to filter any results in GUI nor buisness layer. I also don't have to worry whether there were any modifications to the database between find() being executed and filtering.
    For the GUI. I have two text fields. One for name and one for location.
    If both are blank then I return all records.
    If one of them is blank I return records that exactly match a criterion in the other.
    If both are filled I return records that match both of the criteria exactly.

    What do you think?


    Formely Pawel Pawlowicz
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Urly bird find method: are search criteria "and"- or "or"-composed?