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

Help me check my criteriaFind method

Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Hi everybody here! I began to work out my assignment almost 3 weeks. Now I am blocked by criteriaFind. I use StringTokenizer to slit a String by "," & "=", and then use loop to compare for getting result. I compile my criteriaFind successfully, but when I run it that prompts an error: java.lang.ArrayIndexOutOfBoundsException.
Please help me check what's wrong of it and the search logic is correct or not. Thanx!!!
public synchronized DataInfo[] criteriaFind(String criteria) throws DatabaseException
{
ArrayList aList = new ArrayList(1);
DataInfo[] result;
DataInfo[] temp = new DataInfo[1];
invariant();
int i = 0;
StringTokenizer st = new StringTokenizer(criteria,",");
int number = st.countTokens();
String[] inputData = new String[number];
String[] dataValues = new String[number];
while (st.hasMoreTokens())
{
for (i=0; i<number; i++)
{
inputData[i] = st.nextToken();
StringTokenizer st1 = new StringTokenizer(inputData[i],"=");
dataValues[i] = st1.nextToken("=");
}
}
try
{
seek(1);
DataInfo rv = null;
String [] values = null;
boolean found = false;
int r1;
int r2;

for (r1=1; r1<=recordCount; r1++)
{
values = readRecord();
for (r2=0; r2<number; r2++)
{
if ((values!=null) && (values[r1].equals(dataValues[r2])))
{
found = true;
break;
}
}
}
if (found)
{
rv = new DataInfo(r1, description, values);
aList.add(rv);
}
if (aList.size() > 0)
{
result = (DataInfo[])aList.toArray(temp);
}
else
{
result = null;
}
return result;
}
catch (IOException e)
{
throw new DatabaseException(UNEXPECTED + e);
}
}


SCJP<br />SCJD
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
A stacktrace will help me to help you
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Not knowing enough information, what happens when you change the following to

make r1 = 0.
Just a stab at it.
Mark


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
Sai & Mark, Thanks you all for quick response! In my code, at the 1st paragraph, I intend to split criteria into StringTokenizer, at the 2nd paragraph, I want to compare the criteria with record one by one, and then, at the 3rd paragraph, the result will collecting into a ArrayList and return. I believe the 2nd paragraph has something wrong, but I can not figure out it, and guess it results in the error: java.lang.ArrayIndexOutOfBoundsException
Can you help me deal with it? Thanx a lot!
Adam Caldwell
Greenhorn

Joined: Mar 27, 2002
Posts: 17
I think you need to go back and re-read the requirements and then rewrite your code because you aren't implementing the requirements corectly (irregardless of the fact that you have a bug in your code).
The requirement is to parse something of the form key1=val1,key2=val2,key3=val3 and see if any records match it.
What you have written is something that breaks up the string into key=value strings and then compares the value to the n2-ith data value returned from readRecord().
You need to first figure out which indexes key1, key2, and key3 map to in the array returned by readRecord and then string compare the value at that index with the n-th value string.
Also, if you have any further problems, be sure to include which line number you are getting the error on so that we can help you track down your error faster.
One other thing, you should also add some System.out.println() to your code to print out what tokens it's parsing... If you do that, you'll get a clearer idea of what your program is doing.
-Adam
[ June 02, 2002: Message edited by: Adam Caldwell ]
Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Thank you Adam! Refer to your suggestion, I modified my criterialFind method. This time compiling is fine and no error prompt when it ran. I added some System.out.println() method refer to your suggestion for testing purpose.
System print out:
StringTokenizer fieldValues[i] =Carrier
StringTokenizer dataValues[i] =SpeedyAir
StringTokenizer fieldValues[i] =Origin
StringTokenizer dataValues[i] =SFO
result:null
It seem that line1-25 works ok, but I still did not get the result. What's wrong?
The code shows below:
1.public synchronized DataInfo[] criteriaFind(String criteria) throws DatabaseException
2.{
3.ArrayList aList = new ArrayList(1);
4.DataInfo[] result;
5.DataInfo[] temp = new DataInfo[1];
6.invariant();
7.int i = 0;
8.StringTokenizer st = new StringTokenizer(criteria,",");
9.int number = st.countTokens();
10.String[] inputData = new String[number];
11.String[] fieldValues = new String[number];
12.String[] dataValues = new String[number];
13.int[] fieldIndex = new int[number];
14.while (st.hasMoreTokens())
15.{
16.for (i=0; i<number; i++)
17.{
18.inputData[i] = st.nextToken();
19.StringTokenizer st1 = new StringTokenizer(inputData[i],"=");
20.fieldValues[i] = st1.nextToken();
21.System.out.println("StringTokenizer fieldValues[i] =" + fieldValues[i]);
22.dataValues[i] = st1.nextToken("=");
23.System.out.println("StringTokenizer dataValues[i] =" + dataValues[i]);
24.}
25.}
26.
27.int ii = 0;
28.for (int i1=0; i1<number; i1++)
29.{
30.for (int i2=0; i2<description.length; i2++)
31.{
32.if (fieldValues[i1].equals(description[i2].getName()))
33.{
34.fieldIndex[ii] = i2;
35.ii++;
36.}
37.}
38.}
39.
40.try
41.{
42.seek(1);
43.DataInfo rv = null;
44.String [] values = null;
45.boolean found = false;
46.int r1;
47.int r2;
48.
49.for (r1=1; r1<=recordCount; r1++)
50.{
51.values = readRecord();
52.boolean mark = (values!=null);
53.for (r2=0; r2<fieldIndex.length; r2++)
54.{
55.mark = mark && ((values[fieldIndex[r2]].trim()).equals(dataValues[r2]));
56.}
57.if (mark)
58.{
59.found = true;
60.break;
61.}
62.
63.}
64.
65.if (found)
66.{
67.rv = new DataInfo(r1, description, values);
68.aList.add(rv);
69.}
70.if (aList.size() > 0)
71.{
72.result = (DataInfo[])aList.toArray(temp);
73.}
74.else
75.{
76.result = null;
77.}
78.System.out.println("result:" + result);
79.return result;
80.}
81.catch (IOException e)
82.{
83.throw new DatabaseException(UNEXPECTED + e);
84.}
85.}
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
1) You need to use the char apos (') along with the char equal sign (=) while using StringTokenzier in line 19
2) If the column name is invalid, your fieldIndex array will not have that column index after the line 38. But you are using the size of this array to check the values in line 55. This will not work if you pass a wrong column name.
3) If you find a matching value, you are leaving the loop in line 60. So once a matching record is found, you will always get only one record in the result.
Richard Phen
Ranch Hand

Joined: Apr 27, 2002
Posts: 52
Hi Sai, Thank you for your reply. I have midified line 19 according to your suggestion. For your suggestion 2 & 3, could you give me a more detail explain. Actually, I don't have ideal about how to compare and seach required data from db.db. I am looking forwared for your reply.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help me check my criteriaFind method
 
Similar Threads
criteriaFind().. implementation
Please review my criteriaFind method
How to do exact match in criteriaFind?
about criteriaFind
FBN: criteriaFind(String criteria) method; is this ok ?