• 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 ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

Build a boolean expression from a String : CriteriaFind(String criteria)

Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I urgently need your help.
In the FBN assignement, to load data from tha db.db table and make thinks easy enough to use, I have created a Class to encapsulate the data. (the class has the same fields as the table fields,and so I can easily load the database into an object of my class that I then add to a vector : doing so makes searching easy as it is just a mater of comparing user entered data to class fields.) But I am stuck with criteriafind : The user enters the criteria in a text boxe and I get the creteria fron there. Now using a StringTokenizer, I can parse the text to get the different fields and values. Now I want to use these fields and values to build a boolean expression eg if(myflight.Origin.equals("SFO") && myflight.Price < 1000 && ...)). I have no idea of how to do that easily (I don't want to have to embricated if statements to build a boolean expression for each possible search criteria). I have already tried to build the "string" in the expression. This works fine and I can get the expression as a String (ie , after parsing creteria, I will have the String :myflight.Origin.equals("SFO") && myflight.Price < 1000 && ...). How can I go from that string to a boolean that will execute in an "if" statement ?
Thanks for help.
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you want to create a class that encapsulate the data from the database? Remember that this make your design fixed to records of just one format.
I'm not done with my assignment yet, but I have implemented criteriaFind in the following way:
* Parse the key/values and put them in a SortedMap(for later searching)
* Copy the keys and values in the map to two string arrays (for comparing with values in DataItem)
* Loop through the records and search for matches in field names.
* When an equal field name is found, compare the value of value for this field/key with the one from the criteria.
* If all key/value pairs in a DataInfo object matches all key/value pairs in the criteria, return the DataInfo object.
Hope this helps
[ April 08, 2002: Message edited by: Geir Morten Hagen ]
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The requirements only specify that you need to do equality comparisons in the very specific format that they specify.
What this requirement really means is that they want to make sure that you know how to use string tokenizer.
Here's how I implemented it:
1) complexFind(ArrayList). Took an ArrayList of key value pairs (key = integer id of field, value=string to compare to). The implementation of this function was actually copied from find() and modified to look for more than one key. find() was rewritten to call complexFind.
2) criteriaFind(String) tokenized the string and created the ArrayList to pass to complexFind.
raphael Bereh
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question about using an Object to encapsulate data. My reasons :
1- Java is an Object Oriented Language, and as such, any good Java programming should be manipulating data through Objects.
2- There is no problem about supporting a database schema extension or something else, the data object class can always be extended as in any OOP modelling.
3- Using an Object to manipulate data got from the database eases the manipulation and makes these manipulations clear for someone that has to modify the source code. Take a look at the following code for instance and note how easy the data in : origine list box, destination list box(these are parsed to get unique elements in the list), table header(columns):nothing is a "magical" constant or data, but evrything commes fron the database .
//import suncertify.Client.* ;
import java.lang.*;
import java.util.*;
import java.io.*;

public class fbnmodel extends Observable {
public Vector selection , origine, destination , tabledata , columnsize ;
public static Vector tableheader;
public String Status = null;
flightinfo myrec ;
public fbnmodel(){
origine = new Vector();
destination = new Vector();
selection = new Vector();
tabledata = new Vector();
tableheader = new Vector();
columnsize = new Vector();
Data mydata = new Data("db.db");
FieldInfo [] myinfo = mydata.getFieldInfo();
/* get table header information */
for(int k=0; k<myinfo.length; k++){
tableheader.addElement(new String(myinfo[k].getName()));
//columnsize.addElement(new Integer (myinfo[k].getLength()));

for(int i=1; i<=mydata.getRecordCount(); i++){
/* get records from database and encapsulate them in a flightinfo object */
DataInfo mydatainfo = mydata.getRecord(i);
int recnbr = mydatainfo.getRecordNumber();
String[] test = mydatainfo.getValues();

selection.addElement(new flightinfo(recnbr, test));
} catch ( IOException ex) {
} catch(DatabaseException dex ){
for(int j=0; j< selection.size(); j++){
//boolean exists = false ;
myrec = (flightinfo) selection.elementAt(j);
int existso = origine.indexOf(myrec.Origin_airport);
int existsd = destination.indexOf(myrec.Destination_airport);
if(existso == -1){
origine.addElement(new String(myrec.Origin_airport));
if(existsd == -1){
destination.addElement(new String(myrec.Destination_airport));
origine.insertElementAt(new String(" "),0);
destination.insertElementAt(new String(" "),0);

/*for(int k=origine.size(); k>=0; k--){
String temp = (String)origine.elementAt(k);
for(int t=origine.size(); t>=0; t--){
public Vector getSelection(){
return selection ;
public Vector getOrigine(){
return origine ;
public Vector getDestination(){
return destination ;
public static Vector getTableHeader(){
return tableheader;
public Vector getTableData(){
return tabledata ;
public Vector getColumnSize(){
return columnsize;
public void setStatus(String Message){
Status = Message ;
notifyObservers(new String(Status));
class flightinfo {
public int recordnumber ;
public String Flight_number ;
public String Origin_airport ;
public String Destination_airport ;
public String Carrier ;
public int Price ;
public String Day ;
public String time ;
public String Duration ;
public int Available_seats ;
public flightinfo(int recnum, String[] values){
this. recordnumber = recnum ;
this.Flight_number = values[0].trim();
this.Origin_airport = values[1].trim();
this.Destination_airport = values[2].trim();
this.Carrier = values[3].trim();
//String test = values[4];
this.Price = Integer.parseInt(values[4].trim() );
} catch(NumberFormatException nex){
System.out.println("Error while parsing the price :" + nex.getMessage());
this.Day = values[5].trim();
this.time = values[6].trim();
this.Duration = values[7].trim();
this.Available_seats = Integer.parseInt(values[8].trim());
} catch(NumberFormatException nex2){
System.out.println("Error while parsing number of seats :" + nex2.getMessage());

There are many other reasons and we should discuss about this I think ...
Now , I can see how to store the criteria in a Map but now how to implement the data searching : Remember I am using Object to load the data, so I will have to compare the "query" in the map to some fields of the record.
Need more help to get this clear.
pie. tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
    Bookmark Topic Watch Topic
  • New Topic