• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

search results filtering

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 https://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
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please click on this link, the link in above reply does not work.
https://coderanch.com/t/181678/java-developer-SCJD/certification/FYI-SDK-versions-exam
Thanks
Ravi
 
I am Arthur, King of the Britons. And this is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic