• 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

method criteriaFind

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
here is the criteriaFind I did, I have an error when I do

rv[j++] = new DataInfo(j, description,values);

/******************/
// criteria format
//FlightNO='PA001'[,Destination='JFK',..]
public synchronized DataInfo[] criteriaFind(String criteriaString) throws DatabaseException {
invariant();
try {
System.out.println("try finding"); //###
//##########
//tokenize and parse the criteria
StringTokenizer criteriaTokens = new StringTokenizer(criteriaString,",");
int tokenCount = criteriaTokens.countTokens();
System.out.println("tokencount="+tokenCount);
//the field need to be matched in each flight
//if need match, set to 1
int filedToMatch[] ={0,0,0,0};
String valuesToMacth[]={"", "", "", ""};
while (criteriaTokens.hasMoreTokens()){
//get the criteria pair and trim
String criteriaPair = criteriaTokens.nextToken().trim();
//System.out.println("nextToken="+criteriaPair);
int equalSignPos = criteriaPair.indexOf("=");
String criteria = criteriaPair.substring(0,equalSignPos);
//System.out.println("criteria=" + criteria);
int valueStartIndex = equalSignPos+2;
int valueStopIndex = criteriaPair.length()-1;
String criteriaValue = criteriaPair.substring(valueStartIndex,valueStopIndex);
//System.out.println("criteriavalue="+ criteriaValue);
if (criteria .equals("FlightNo")) {
filedToMatch[0] = 1;
valuesToMacth[0] = criteriaValue;
} else if (criteria.equals("Origin")) {
filedToMatch[1] = 1;
valuesToMacth[1] = criteriaValue;
} else if (criteria.equals("Destination")) {
filedToMatch[2] = 1;
valuesToMacth[2] = criteriaValue;
} else if (criteria.equals("Carrier")) {
filedToMatch[3] = 1;
valuesToMacth[3] = criteriaValue;
}
}
for (int i=0; i<4; i++){
System.out.println("filedToMatch" + i + "=" + filedToMatch[i]);

}
//##########
seek(1);
DataInfo[] rv = null;
String [] values = null;
boolean found = false;
int r;
int j=0;
for (r = 1; r <= recordCount; r++) {
values = readRecord();
System.out.println("search for record no." + r);
if (values != null) {
found = true;
for (int i=0; i<4; i++) {
//System.out.println("enter for loop");
if (filedToMatch[i] == 1){
//System.out.println("compare now");
found= found && (values[i].equals(valuesToMacth[i]));
//System.out.println("found= " + found);
}else{
//System.out.println(" skip compare");
}
}
}
//if(found) break;
if (found) {
System.out.println("I find it, yahhhhhh, yehhhhh "); //###
rv[j++] = new DataInfo(j, description, values);
System.out.println("OK");
}
}
/*
if (found) {
rv = new DataInfo(r, description, values);
System.out.println("I find it, yahhhhhh, yehhhhh "); //###
}
*/ return rv;
} catch (IOException e) {
throw new DatabaseException(UNEXPECTED + e);
}
}
/***************/

What is the fix for this?
Could someone tell me if the way to parse the criteria OK?
Thanks.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Allen,
I would implement a more generic form of criteriaFind, because the assignment says that
the application should be extensible in the future.
I did my criteriaFind covering every column of the database schema.
E.g. you could pass a criteria like
flight='SA001',origin='SFO',destination='DEN',carrier='SpeedyAir',price='400',day='Sun',time='13:40',duration='20m',seats='50'
First I use the array dataInfo returns and look up any occurrence of the field name in the
criteria String, this way I get each seach criteria very easy (no StringTokinzer needed).
Then I build the mapping for column number to search criteria, which I put into a HashMap.
The resulting map looks e.g similar to this {3=DEN,1=SFO,0=SA001}.
I have a private method matchFind which takes the map as argument and returns a List.
Now I know exactly in which columns I have to search and the criteria for the specific column.
The rest is just looping through all records and comparing the columns and criterias which
I have in my map with the corresponding column and entry in the received record.
Doing it in this fashion I think is a more general approach to solving the problem and
it works with different database table schemas as well.
Hope this helps.
jay
 
jay denzel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing to mention.
You can not dynamically resize arrays,
e.g.
DataInfo[] r = new DataInfo[x];
for (int i=0; i < 10; i++)
r[i] = new DataInfo( .. )
is not possible, that is why you get an error.
why not return a List
public synchronized List criteriaFind(...
and put the DataInfo objects into the List.
jay
 
Allen Chan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jay,
Thank you very much for your reply. That is a good idea. However, still could not figure out how to use map (put criteria in map?), then how you do the compare?
Thanks again for your clue.
 
Popeye has his spinach. I have this 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