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


JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "criteria string" Watch "criteria string" New topic
Author

criteria string

Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Hello,
Do I have to use 3 parsers for the criteria string ... for "," and "=" and "'"?
Also I have extracted the field names and values from the criteria strings using arrays. Do I need to use maps to compare values?
Thanks


Discipline, Dedication and Determination define Destiny.
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Nandini,
I used regular expression to parse the searchQuery string. It was hardly few lines to parse the query.
Query pattern I followed:
name1="value1", name2="value2" ,name3="value3" ...
expression will split the string into name & value pairs, which I store directly into a Map collection object, and I search in the database accourdingly.
I took the help of Andrew while doing so.
Regular expression, I took from one of the posts here, and was given by Jim.
Good luck.
Regards,
Ganapathy.
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Hai,
Do I have to use either Regular Expressions or Tokenizers for parsing the string, or can I just replace the characters "'"and "=" and "," and get a final criteria string without these delimiters?
Are there any methods that I am supposed to use at any cost anywhere.. for example, I have to use either RMI or Serialization in my assignment.
Thanks
Nandini
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Well.. I am using a mix of tokenizers and replacing methods.. Is that fine?
S. Ganapathy
Ranch Hand

Joined: Mar 26, 2003
Posts: 194
Hi Nandini,
RMI is a proven distributed architecture. Even EJB uses RMI underneath. Moreover, developer need not worry about multithreading, an advantage. Ofcourse serialization is also takencare by the RMI. With its counterpart, developers effort is needed, and it may be error prone. Choosing RMI approach is a wise decision, I feel. Ofcourse this is the question to put across the gurus here.
Comming to regular expression, I tried with StringTokenizer, and String's split(regex) methods. But I am comfortable with regular expression, but it is totally a taster's (implementor's) choice.
Let me take , the criteria string as:
name="ganapathy", post="SCJD", site="JavaRanch"
If I use StringTokenizer("\","); The tokenizer splits the tokens to name value pairs. Means, it removes " and , but if the developer want to remove ", consecutively, there is an adverse effect as well. Like if the string is name="ganapathy" post="SCJD,JavaDeveloper", ...
there is some problem in getting tokens as it removes , from SCJD,JavaDeveloper.
So implementor should choose the splitting carefully. I feelString's split() method is better, and again it is implementor's choice.
Goodluck.
Regards,
Ganapathy.
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Thanks Ganapathy, for ur suggestion. Let me tell how I have sorted out this method.
1. Parse the criteria string using tokenizer for the delimiter "'".
2. Store the parsed string in a vector, get the field names and column values from the vector and add it to a map, MAP1(field names(key)).
3. Reading the record
for (...) { // begin for loop
a. Read a record;
b. Store the field names and values read in another map (MAP2);
c. Iteration begins:
Iterate over the keys in MAP1 and check for the existence of every key in MAP2.
If MAP2 contains both the key and the corresponding value for that key, (....coding here..... and finally "list.add(DataInfo object)"
} // End for loop
4. Convert the list to DataInfo array and return the array.
I have some doubts regarding this method:
1. Please tell me how efficient my method is.
2. I have used toLowerCase() to take care of the field names and values. Is that ok?
3. Should my method disregard the field, "Origin airport" if it is just "Origin" in the criteria string???
4. Should my method disregard "flight number" if it is going to be "flightnumber" ?? (without spaces in between)
Thanks
Nandini
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Nandini
1. Please tell me how efficient my method is.

Not very
But efficiency is not a criteria for this assignment. Clarity and maintainability is more important. So I really wouldnt worry about the many ways that your code could be made more efficient.
I have used toLowerCase() to take care of the field names and values. Is that ok?

I wouldn't. There is the requirement: "In the event of an invalid field name being provided as part of the criteria the behavior of this method is the same as if no records matched correctly specified criteria" - I think receiving field names or values in the wrong case qualifies as invalid data.
Also - the "user must be able to describe enter the string value "any" for the (criteria)". I chose to handle this at the client side - if the user specified "any" then that criteria was not sent to the criteriaFind method. But if you want to handle it at the server side as well, then you are potentially creating a problem for yourself. "ANY" is a valid airport code (Anthony Municipal Airport, KS, USA). If you receive criteria "ANY" and convert it to lowercase "any" then discard it because it matches the magic string, then you are not giving the client what they expect.

3. Should my method disregard the field, "Origin airport" if it is just "Origin" in the criteria string???
4. Should my method disregard "flight number" if it is going to be "flightnumber" ?? (without spaces in between)

Yes. Refer to the instruction: "In the event of an invalid field name being provided as part of the criteria the behavior of this method is the same as if no records matched correctly specified criteria"
Of course it doesn't help that the example provided by Sun in their instructions would not work. But the instructions are clear - the code has to get field name correct.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Thanks for ur reply, Andrew.
1. I agree with your point on "toLowerCase()" issue. But that's only for field names? Can the string values can be converted to their lowercases?
2. Do we have to consider the customer's choice of time of flight .. say he wants to travel on July 20, late evening.. do we have to then provide him with only those options that satisfy his time of travel also? In that case, we need to provide him with another combo box for the travel "dates" and "timings", right?
Thanks
Nandini
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Nandini
1. I agree with your point on "toLowerCase()" issue. But that's only for field names? Can the string values can be converted to their lowercases?

I wouldnt convert either option to lowercase. But this is your choice.
2. Do we have to consider the customer's choice of time of flight .. say he wants to travel on July 20, late evening.. do we have to then provide him with only those options that satisfy his time of travel also? In that case, we need to provide him with another combo box for the travel "dates" and "timings", right?

I dont think we need to do this.
The specifications only require Origin and Destination airport codes. Many people have passed with only those two options.
The example also shows the Carrier, so many people have also given a combo box for this (myself included).
But beyond those two (or three) fields, I would not offer any other combo boxes.
However on the server side, your criteriaFind() method should be written such that if a later requirement meant that someone was searching for a value in another field, then it should be achievable. But just do string searches: dont try and treat the string formatted dates as real dates.
Regards, Andrew
Venkatesh Krishnappa
Greenhorn

Joined: May 26, 2003
Posts: 16
hey Andrew/Nandini,
In my instructions.html, I see under section marking
Server Design (Total 53)
locking (30)
error handling (8)
search algorithm: clarity and efficiency (15)
looks like the *efficiency* factor counts for criteriaFind() implementation alone.
-venky
Nandini Sriram
Ranch Hand

Joined: Jul 04, 2003
Posts: 132
Thanks for bringing it to notice, Venkatesh. I think I'd just go ahead for now and make the whole thing work. Then probably I can think of better solutions
I have a table model class that performs the 'criteria string formation'. How I have done it is -
1. Define a few constants:
public static final int fieldONE = 1;
public static final int fieldTWO = 2;
2. Get the field names from the getFieldInfo() using these constants.
4. String criteria = field1+"="+"'"+c1+"'"+","+field2+"="+"'"+c2+"'";
where c1 and c2 are the origin and the destination chosen by the customer.
5. DataInfo [] diArray = dt.criteriaFind(criteria);
6. And I finally display the results in a table(taken care of by another class that uses this model)
Q1. Is it ok to define specific constants (pertaining to the field numbers)like how I have done? I am not harcoding anything anywhere in my Data class.
Q2. Also I have an option "Any Place" in my origin and destination combos. So when that is selected (for Origin and Destn), it's handled in the table model class itself (and all the flight schedules are displayed) and not taken to the Data class for criteriaFind().
Please give me your suggestions.
Thanks
Nandini
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11465
    
  94

Hi Nandini
I'd like to add my thanks to Venkatesh for pointing out that marking comment.
Is it ok to define specific constants (pertaining to the field numbers)like how I have done?

You should have the requirement: "Criteria take the form of a comma separated list of <field name>=<value to match> specifications". So you should be using the field names not their position in the database.
Also I have an option "Any Place" in my origin and destination combos. So when that is selected (for Origin and Destn), it's handled in the table model class itself (and all the flight schedules are displayed) and not taken to the Data class for criteriaFind().

I also trapped "any" in the client side - it did not get sent through to the server side. I also had the server side check for and discard the "any" if some other client decided to sent it through.
The reason I used "any" was so that it matched the three letter format of all the other options the user could choose (plus it is the text used in the instructions).
Regards, Andrew
 
GeeCON Prague 2014
 
subject: criteria string