aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes method criteriaFind 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 "method criteriaFind" Watch "method criteriaFind" New topic

method criteriaFind

Allen Chan
Ranch Hand

Joined: Jun 07, 2002
Posts: 64
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?
jay denzel
Ranch Hand

Joined: Sep 18, 2002
Posts: 57
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

Joined: Sep 18, 2002
Posts: 57
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

Joined: Jun 07, 2002
Posts: 64
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.
It is sorta covered in the JavaRanch Style Guide.
subject: method criteriaFind