wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes search results filtering Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "search results filtering" Watch "search results filtering" New topic
Author

search results filtering

ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
I am using the following code for filtering the search results but it doesn't seem to give the right results as I find that results returned for say origin='SFO' , destination='Any' , carrier='Any' doesn't seem to limit the search results to all the flights originating from 'SFO' rather the search results after flitering doesn't add any results to the vector.
info = m_client.criteriaFind(criteria);
if ( (info != null ) && (info.length > 0) ) {
// data found
m_result = 0;
m_vector.removeAllElements();
for ( int i = 0; i < m_count; i++ ) {
if ( info[i] != null ) {
m_data = info[i].getValues();
m_flightID = m_data[0];
m_origin = m_data[1];
m_destination = m_data[2];
m_carrier = m_data[3];
m_price = new Double(m_data[4]);
m_day = m_data[5];
m_time = m_data[6];
m_duration = m_data[7];
m_seats = new Integer(m_data[8]);
fbn = new FBNData(m_flightID, m_origin, m_destination, m_carrier, m_price, m_day, m_time, m_duration, m_seats);
// filtering code
if ( m_origin.equals("Any") && ! m_destination.equals("Any") && !m_carrier.equals("Any")) {
if (m_destination.equals(destination)&& m_carrier.equals(carrier))
m_vector.addElement(fbn); }
else if (!m_origin.equals("Any") && m_destination.equals("Any") && !m_carrier.equals("Any")) {
if (m_origin.equals(origin) && m_carrier.equals(carrier))
m_vector.addElement(fbn); }
else if (!m_origin.equals("Any") && !m_destination.equals("Any") && m_carrier.equals("Any")) {
if ( m_origin.equals(origin) && m_destination.equals(destination))
m_vector.addElement(fbn); }
else if (m_origin.equals("Any") && m_destination.equals("Any") && !m_carrier.equals("Any")) {
if ( m_carrier.equals(carrier))
m_vector.addElement(fbn); }
else if (m_origin.equals("Any") && !m_destination.equals("Any") && m_carrier.equals("Any")) {
if (m_destination.equals(destination))
m_vector.addElement(fbn); }
else if (!m_origin.equals("Any") && m_destination.equals("Any") && m_carrier.equals("Any")) {
if ( m_origin.equals(origin))
m_vector.addElement(fbn); }
else if (!m_origin.equals("Any") && !m_destination.equals("Any") && !m_carrier.equals("Any")) {
if (m_origin.equals(origin) && m_destination.equals(destination) && m_carrier.equals(carrier)) m_vector.addElement(fbn); }
} } } }
Any help is appreciated.
Thanks
Ravi


SCEA, SCBCD, SCWCD, SCJD, SCJP
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ravi,
I am somewhat unclear on your filtering and searching algorithms but let me say that there are much simpler ways (and much clearer too) to do it. First of all, don't use Vectors. You should use the newer Collection API such as HashMap, TreeSet, etc. All of your filtering should probably be done in the criteriaFind() method through the public Data interface. I just returned an array of DataInfos, but it could be a List or Set(List is probably better since it will be in order) just as well. My whole criteriaFind() method involved two StringTokenizers and about fifty lines of code total.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
I have a few things to say:
1. Use the CODE tag when you post code here, otherwise it is impossible to read.
2. Forget the underscore and the prefixes (such as "m") in the variable names, read the Java Coding Conventions
3. Rethink your logic when handling the "any" parameters, -- the entire segment of code that you posted is redundant. Here is a hint: you only need to check for field values that are not "any".
4. You don't need to search the flights by "Carrier". While your criteriaFind() method should be generic, the flight search feature should only take "origin" and "destination".
Eugene.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
4. You don't need to search the flights by "Carrier". While your criteriaFind() method should be generic, the flight search feature should only take "origin" and "destination".

There is an implied requirement to search by carrier in the instructions in the example:
"Carrier='SpeedyAir',Origin='SFO'"

I know many (including myself) did include the carrier as a criteria, although it is probably unnecessary as an absoulte requirement as you have indicated that you did not include it and still made a very high score. The key is to make your criteriaFind algorithm generic so that it will work with any field or schema for that matter. And as you said the trick is to ignore the "anys" and just don't include any record that doesn't match the explict criteria in your search results.
Michael Morris
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Micheal/Eugene
My criteriaFind method implementation is as follows:
public DataInfo[] criteriaFind(String criteria) {
DataInfo temp = null;
int count = this.getRecordCount();
DataInfo[] results = new DataInfo[count];
DataInfo[] dataInfo = new DataInfo [count];
String[] record = new String[9];
StringTokenizer tokens = new StringTokenizer
(criteria, DELIMITERS);
int numberOfKeys = tokens.countTokens() / 2;
String[] fields = new String[numberOfKeys];
String[] values = new String[numberOfKeys];
for (int i = 0; i < numberOfKeys; i++) {
fields[i] = tokens.nextToken();
values[i] = tokens.nextToken();
}
for ( int i = 0; i < count; i++ ) {
try {
if ( null != ( temp = this.getRecord
(i+1) ) ) {
dataInfo[i] = temp;
record = dataInfo[i].getValues();
for ( int j = 0; j < record.length;
j++ ) {
for ( int k = 0; k <
values.length; k++ ) {
if ( record[j].equals(values
[k]) ) {
System.out.println("match");
results[i] = dataInfo[i];
} else {
System.out.println("no
match");
}
}
}
} // end of if
} catch (DatabaseException e) {
}
}
return results;
}
Does that mean, I need to rework on my criteriaFind method. Right now, It works on the criteria and returns all the records which meet the criteria. However, It doesn't take care of 'Any' value as the comparision of the selected values is to the values in the database and database doesn't have 'Any' value so I have to handle this comparison on the client side to display the correct records. Is this approach not correct ?
Thanks
Ravi
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ravi,
First of all it would be much easier to analyze your code if you would enclose it with the "CODE" button underneath the text box for posting. Best I can tell you need to rework your algorithm so it will handle "anys". You are starting from the assumption that a record does not match the criteria. Invert that assumption, ie assume that all records match and then remove them from the results when they don't. That way you don't have to concern yourself with the "anys", they'll just be there by default. You shouldn't have a criteria like "Destination='ANY'", just criteria with actual values for this approach to work.

Hope this helps,
Michael Morris
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Michael
I was just wondering how to post the code with CODE tag. I shall definately do so in future. Thanks for educating me.
I shall rework on my criteriaFind method to handle 'ANY' in the method itself.
Thanks
Ravi
ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
Hi Micheal
The reason being my using Vector over a HashMap or a TreeSet is that the JTable constructor allows for Vecor as a parameter in its constructor so if I go for a HashMap or a TreeSet, I need to write additional lines of code to convert a HashMap or a TreeSet into a Vector for displaying the records in a JTable.
Thanks
Ravi
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ravindra,
That's true if you just go with the default model, but if you create your own table model (extending AbstractTableModel) your table data can be anything you want. Mine was an array of FBNDataInfo which wrapped DataInfos. You may want to consider creating your own table model. It's really fairly easy and will give you more options on manipulating your model and table.
Hope this helps,
Michael Morris
Gurpreet Saini
Ranch Hand

Joined: Jun 09, 2002
Posts: 295
Hi reader,
Do you guys are implementing criteria somewhat like this:
"Carrier='SpeedyAir'"
I wanted to know are you using single quotes also as they are on SpeedyAir ?.
Secondly, my criteriaFind() method can take any values and can evaluate the results. But the assignment has 15 credits for Search Algorithm. Over here I would say that there are generally two types of algorithm one is sequential (The one on which I am relying) the other one is Binary. Has here any guy had implemented Binary search. And if yes then how ?.
Thank you,
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi ravindra,
If you're using java 1.4, or if you're willing to use it, your search becomes a trival matter, because you can use regex.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Hi Max
I studied regex and I think it is a powerful feature and it would remove lots of lines of code in my search algorithm. However, if I use regex, I would have to use the development version of JDK i.e 1.4.0_01 version. However, please refer to this thread regex http://www.coderanch.com/t/181678/java-developer-SCJD/certification/FYI-SDK-versions-exam, according to which Sun is asking people to use JDK 1.3. I am confused on this aspect. Could we use JDK 1.4.0_01 or we are restricted to use JDK 1.3 only.
Thanks
Ravindraweb page


SCJP, SCJD, SCWCD, SCBCD, SCEA
ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
Please click on this link, the link in above reply does not work.
http://www.coderanch.com/t/181678/java-developer-SCJD/certification/FYI-SDK-versions-exam
Thanks
Ravi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: search results filtering
 
Similar Threads
CriteriaFind Question regarding string
Search Criteria
search algorithm: clarity and efficiency
getRecord Vs readRecord usage in criteriaFind()
search criteria