Vera said:
I'm trying to add together all the contents of the vector. This is what
I wrote and the compiler is telling me I can't use the plus. Is there
another way?
for (int count = 0; count < vectorSize; count++)
{
sum += tokens.elementAt(count);
}
Hi!
For such questions write the complete error message of the compiler!
(copy&paste)
What type is sum?
What type are the elements of your vector "tokens"?
Which java-version do you use? If you use 1.5, do you use generics?
I think the problem may be, your vector contains Objects, because you
did not use generics and did not specify the Vector only to accept
Strings (and you want to use Strings in the vector).
+ is not applicable for objects.
If you are not using java 1.5, but lower, you cannot use generics. In
that case you have to do a type cast:
for(int count=0;count<vectorSize;count++) {
sum += (String)tokens.elementAt(count);
}
(Of course only if sum is a String...)
Think your code can be improved, too.
If you want to iterate over the vector, I think it is better not to use
an extra variable vectorSize, but use tokens.size() instead.
If you use java 1.5, you can also write: (if the vector contains only
Strings and you are using generics)
Vector<String> tokens = new Vector<String>();
tokens.add(...);
....
String sum="";
for (String s:tokens) {
sum+=s;
}
Something like that.
Hope that helps!
bye, Ralf