Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How to search a Vector array?

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help. But, if the variables are double, it does not work.
Does the compareTo() compare double?
Thanks
 
Pete Lyons
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic