Perry Smith

Java Questions & Answers

Home / Java Q & A Archive Page 3 / Java Q & A Archive

StringTokenizers and vectors
Saturday, 06-Feb-99 18:56:23

128.171.20.119 writes:

I have to parse input (an int and a String)via StringTokenizer and put it into a vector for manipulation (ie, binary searches, find mins and maxs, etc.). How does it store into a vector? As 1 part or two distinct parts? Can I find the value of the string associated with the int that it's stored with?

Any help someone can give would be hugely appreciated!

Mick C


Response
Re: StringTokenizers and vectors
Sunday, 07-Feb-99 10:49:03

152.206.191.202 writes:

The data is stored in the Vector in as many "parts" as you place in the Vector. If you separate the int and string and use separate

 vecMNDT.addElement(Object objToAdd)
      calls for the int and String, then you have two parts per element.

In my humble opinion, the more object-oreiented program design would encapsulate the int and String into it's own little MyNewDataType class with appropriate accessor and modifier methods. Then this new type would be stored in the Vector.

Reading your question again, it looks like you might be storing the StringTokenizer in the Vector. If that's the case then your Vector will only have 1 element, because the StringTokenizer contains your whole message. You have to use

 stInput.nextToken()
      to get the parsed substrings.

One last note, all data that is stored in Vectors are cast to type Object. This means you'll need to explicitly recast them to their original type when you extract the data.

 mndtIntString
      = (MyNewDataType) vecMNDT.elementAt(int iIndex);

MikeD


Home / Java Q & A Archive Page 3 / Java Q & A Archive