aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to do exact match in criteriaFind? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to do exact match in criteriaFind?" Watch "How to do exact match in criteriaFind?" New topic
Author

How to do exact match in criteriaFind?

Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Help!!!
I can use my criteriaFind method to find out some data for db.db, but those is not exact match. I believe the code that at line58-76 has some problem. Would you please help me to find out the problem? Thanks a lot!
1.public synchronized DataInfo[] criteriaFind(String criteria) throws DatabaseException
2.{
3.ArrayList aList = new ArrayList();
4.DataInfo[] result;
5.invariant();
6.int i = 0;
7.StringTokenizer st = new StringTokenizer(criteria,",");
8.int number = st.countTokens();
9.String[] inputData = new String[number];
10.String[] fieldValues = new String[number];
11.String[] dataValues = new String[number];
12.ArrayList fieldtemp = new ArrayList();
13.Integer [] fieldIndex;
14.Integer [] valuesIndex;
15.
16.while (st.hasMoreTokens())
17.{
18.for (i=0; i<number; i++)
19.{
20.inputData[i] = st.nextToken();
21.StringTokenizer st1 = new StringTokenizer(inputData[i],"='");
22.fieldValues[i] = st1.nextToken();
23.System.out.println("StringTokenizer fieldValues[i] = " + fieldValues[i]);
24.dataValues[i] = st1.nextToken("='");
25.System.out.println("StringTokenizer dataValues[i] = " + dataValues[i]);
26.}
27.}
28.
29.for (int i1=0; i1<number; i1++)
30.{
31.for (int i2=0; i2<(getFieldInfo()).length; i2++)
32.{
33.if (fieldValues[i1].equals((getFieldInfo())[i2].getName()))
34.{
35.fieldtemp.add(new Integer(i2));
36.}
37.}
38.}
39.
40.if ((fieldtemp.size())!=number)
41.{
42.System.out.println("Invalid fieldValues");
43.result = null;
44.return result;
45.}
46.else
47.{
48.fieldIndex = (Integer [])fieldtemp.toArray(new Integer [fieldtemp.size()]);
49.try
50.{
51.seek(1);
52.DataInfo rv =null;
53.String[] values = null;
54.int r;
55.int alIndex = 0;
56.boolean mark = false;
57.
58.for (r=1; r<=recordCount; r++)
59.{
60.values = readRecord();
61.mark = (values != null);
62.for (int i4=0; i4<number; i4++)
63.{
64.for (int i5=0; i5<fieldIndex.length; i5++)
65.{
66.if (mark &&(dataValues[i4].equals(values[(fieldIndex[i5]).intValue()].trim())))
67.{
68.rv = new DataInfo(r, description, values);
69.aList.add(alIndex, rv);
70.alIndex++;
71.System.out.println("rv be added to aList");
72.}
73.}
74.}
75.}
76.
77.result = (DataInfo[])aList.toArray(new DataInfo[aList.size()]);
78.return result;
79.} catch (IOException e)
80.{
81.throw new DatabaseException(UNEXPECTED + e);
82.}
83.}
84.}


(jl: added code tags)
(jl: on second thought, removed code tags bec. of hard tabs used in post)
[ June 10, 2002: Message edited by: Junilu Lacar ]
[ June 10, 2002: Message edited by: Junilu Lacar ]

SCJP<br />SCJD
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Your code is hardly readable (use CODE button next time you post, and simplify the below line:

From your code in the line above, it looks to me that you are adding a record to your result set when any condition in your search query is met, rather than when all conditions are met.
Eugene Kononov.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

I think Eugene hit it pretty good there.
The only point I would suggest, is to really break up that method into multiple methods that handle one thing only. This will make your code more readable and maintainable.
For example in your code you have

This part I assume takes the string and tokenizes twice and puts the values into your array. All this code can be moved into a seperate method called getCriteriaArray(String criteriaString){
}
This way you have only one line of code in your actual criteriaFind method to get this array. It also is more readable and you will find more maintainable.
Oh, and remove your call to invariant, you don't need it.
Hope that helps
One more thing, I had a method like this that I added to the Data class, I am not sure, but have some good feelings on this, that I should have put this somewhere else, like in it's own CriteriaSearch class, then in your criteriaFind method you can create an instance of this class, pass the criteriaFind string, and have it go through the work.
Mark
[ June 10, 2002: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Eugene, Thanks for your reply!
you wrote:
From your code in the line above, it looks to me that you are adding a record to your result set when any condition in your search query is met, rather than when all conditions are met.
Yes, how to correct it! Please help!
Andre Mermegas
Ranch Hand

Joined: Jun 20, 2001
Posts: 158
how about looping through and creating an array of matches based on the first criteria,and then looping through the matches,looking for the second criteria and putting that into another array.
etc..this can be done with 2 arrays. one entry and one matches
[ June 10, 2002: Message edited by: Andre Mermegas ]

--=( Andre Mermegas )=--
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Here's the second method of the CriteriaHelper class. Called isRecordAMatch().
You need to take one record, loop through it's fields, if the field name is that same name as any in the criteriaFind string, then check to see if the value in that filed matches what is in the criteria string for that field, if it is then it had that field matching, if it does not then it will not match, and you have found out that this record should not be added to the return array.
I will let you sort all the details out yourself.
Mark
Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Thanks! Let me think about your all hint first and coding second.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to do exact match in criteriaFind?