aspose file tools*
The moose likes Java in General and the fly likes How to search a Vector array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to search a Vector array?" Watch "How to search a Vector array?" New topic
Author

How to search a Vector array?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
I created a text file in which has the data likes name, sex and age.
Tom, M, 23
Mary, F, 29
Peter, M, 30
.......

Vector vector = new Vector();String string = ""; while ((string = in.readLine()) != null ) { StringTokenizer st = new StringTokenizer(string, " "); String name = st.nextToken(); String sex = st.nextToken(); String age = st.nextToken(); vector.add(new Data(name, sex, age));

So, the data in text file will be read to a Data class which has 3 String variables.
Here is the Data class:

public class Data { private String name; private String sex; private String age; public Data(String a, String b, String c){ name = a; sex = b; age = c; } public String toString(){ return(name + " " + sex + " " + age); }}

So, the vector array store each object for each line of record now. Problem occurs.
How can I only print out the name of 3rd line (for example) only, how can I print out name and age of 5th line of record?
When I System.out.println((Data)vector.elementAt(i)) - here has a for loop, it print out the whole line of record becasue I override the String object in the Data class.
However, I need to do a keyword search by name, sex, age and wildcard for them.
When I am using the following code in a for loop to do a search, it cannot find the matching.

vector.elementAt(i).toString().equalsIgnoreCase(consoleinput)

How should I do it?
Thanks for any advice.
Andrew
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
Just add public accessor methods for each property such as
and then use these in your search. Wildcard support will be trickier, you can use a Regular Expression if you are on JDK 1.4 or you can do a more limited version in code, such as only allowing the wildcard character at the end and using String.startsWith() to do the match.
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for help. If I want to do the wildcard search, it should be both "?" or "*" only. It must appear after at least ONE char letter, e.g J* return John, Ton? return Tony, etc.
Should I use startsWidth() or subString() to do it?
Thanks
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Also, I have an age data type to be int in the StringTokenizer and passed it to the vector array, how can I perform a sorting int data type with compareTo() method?

It said object type required. How should I solve it?
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
for an int you can use code such as:

or you can wrap the int into an Integer and call compareTo on that.
As to the wildcards, do the simplest thing that meets the needs of your users. Do your users really care enough about wildcard matching to use '?' instead of '*'? For most users, even '*' is too tricky, and you just always do a case-insensitive startsWith type search.
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Thanks for your help. But, if the variables are double, it does not work.
Does the compareTo() compare double?
Thanks
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
You can't call methods on any primitives in Java (int, short, long, boolean, double, float , and so on). You must use the appropriate wrapper class (java.lang.Double) if you need compareTo, or use the primitive comparison operators as demonstrated above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to search a Vector array?