File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes My search method is not working ( vectors) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My search method is not working ( vectors)" Watch "My search method is not working ( vectors)" New topic
Author

My search method is not working ( vectors)

o rodiguz
Greenhorn

Joined: Sep 30, 2010
Posts: 2

I am implementing a searching method for my first program using Vectors

But it does not work not sure why??



The program runs but the search is unsuccessful.

Thank you for time and interest.
Mos Jeff
Greenhorn

Joined: Sep 15, 2010
Posts: 14
Vector: indexOf(Object elem)
Searches for the first occurrence of the given argument, testing for equality using the equals method.

Your Vector is filled with Customer objects, yet you are trying to look up these objects with Strings, you cannot compare a Customer object with a String object, they are not the same thing.


You could use a Map<String, Customer> to do this. Or you can turn that String value parameter to a Customer object that you can compare with value objects in the Vector.
o rodiguz
Greenhorn

Joined: Sep 30, 2010
Posts: 2
Hi there Thank you for the quick reply

I am very new at java, i understood the comparison problem.( String vs Object).

how can i convert the String value parameter to a Customer object and then compare it with value objects in the Vector ??

i have never used Map<String, Customer> before.




Thank you for time and interest.


Mos Jeff
Greenhorn

Joined: Sep 15, 2010
Posts: 14
What I mean by create a Customer from the String is, there is a constructor that accepts parameters, you use this here.
This creates a Customer object that holds that customers information.

The point of this exercise appears to be that you are to get a customer based on their name. Now the question becomes, have you learned the equals method?

If you have you would override this method to compare Customer objects.

However, you may not have learned this yet, in which case you can identify each customer by name. So you would loop through all of the Customers in the Vector list and get their name. Once you have their name you can compare it with the String name you have passed in to the method.

This is assuming that the text typed in is the name of the Customer you are looking for, I have also assumed you know how to get object properties.

good luck.

for more help I recommend Googling 'java constructors'; 'java String comparison' ; 'java equals method'
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

A Map is interface defines a key value relation. An implementation of that interface is HashMap.
I would advise you to get familiar with Lists, Sets and Maps because you will be using them very often.

Interesting javadocs:
java.util.Map, implementation: java.util.HashMap
java.util.Set, implementation: java.util.HashSet
java.util.List, implementation: java.util.ArrayList


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Wouter Oet wrote: . . . I would advise you to get familiar with Lists, Sets and Maps because you will be using them very often. . . .]
There is a whole section about that in the Java™ Tutorials.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My search method is not working ( vectors)