Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Vector Values 1

Status
Not open for further replies.

dcusick

Technical User
Aug 9, 2000
271
US
Okay, I have a problem that is driving me crazy here... I have a vector with mixed values in it, i.e. String and int. I want to be able to compare the int values with another int variable passed to the function as a parameter. If I use the code

vectorname.elementAt(i) < intCompare

I get an error saying that you can't compare an object with an int. What do I have to do the the vector object to be able to compare it with the int value? Thanks in advance for your help.

Doug
 
You can't store a primitive value (int, long, etc) in a Vector. You can only store objects in a Vector (Integer, Long, etc).

So you can't be doing what you think you're doing. Post a piece of your code so we can see how you're adding elements to the Vector.



&quot;When you have eliminated the impossible, whatever remains, however
improbable, must be the truth.&quot; ~ Arthur Conan Doyle
 
Thanks for the reply. Okay, I stated that wrong. I have an integer value that I'm storing into a vector, by converting it into a string. I realize when I add it to the vector it becomes and object. Here's the code I use to add it...

//***************************
Vector routeTable;
int metric;

routeTable.addElement(Integer.toString(metric));

//****************************

What I'm looking to do, is to be able to retrieve that value that is stored in the vector and compare it to an int variable. Thanks.

Doug
 
If it is a string that is on the vector then you can use Integer.parseInt(string) to convert in to an int. Something like:

Integer.parseInt((String)vectorname.elementAt(i)) < intCompare

This casts the Object on the vecto to a String and then uses parseInt to return the int value.
 
Exactly what I was looking for. Thanks Dekker!

Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top