Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

method criteriaFind

Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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
public synchronized DataInfo[] criteriaFind(String criteriaString) throws DatabaseException {
try {
System.out.println("try finding"); //###
//tokenize and parse the criteria
StringTokenizer criteriaTokens = new StringTokenizer(criteriaString,",");
int tokenCount = criteriaTokens.countTokens();
//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();
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]);

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);
//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);
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?
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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
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 denzel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing to mention.
You can not dynamically resize arrays,
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.
Allen Chan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic