Adding Contents of a Vector

V

Vera

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);
}
 
R

Ralf Seitner

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
 
Z

Zaph0d

Ralf said:
String sum="";
for (String s:tokens) {
sum+=s;
}

Actually, it'll be much better using StringBuilder for this type of a
loop.

String is immutable. Meaning that String s += "1'; will actually create
a new String object and, eventually, gc the old one.
StringBuilder sb = new StringBuilder();
sb.append(s); sb.append("1");
will use the same buffer array (if possible).
Note1: that the buffer is using doubling strategy (if running out of
space, double the array size and copy the old array to the first half
of the new array), so it might be wise to specify initial size, if you
can.
Note2: StringBuilder is not synchronized. If you're multi-threading,
use StringBuffer (which is a tad slower).
 
R

Ralf Seitner

Zaph0d said:
Actually, it'll be much better using StringBuilder for this type of a
loop.
Hi!
You are right.
To be honest, I did not know, there is a StringBuilder. Saw it is new by
1.5 ;-) (Of course I know StringBuffer, which would have been an option,
too)
But there was one thing in my mind:
Had the feeling, the OP is relatively new to java, so I tried to make
the example as easy as possibly.
And... If it is not a String, this loop also works for Integer, etc.
But thanks. Learned something, too. :)
String is immutable. Meaning that String s += "1'; will actually create
a new String object and, eventually, gc the old one.
StringBuilder sb = new StringBuilder();
sb.append(s); sb.append("1");
will use the same buffer array (if possible).
Note1: that the buffer is using doubling strategy (if running out of
space, double the array size and copy the old array to the first half
of the new array), so it might be wise to specify initial size, if you
can.
Note2: StringBuilder is not synchronized. If you're multi-threading,
use StringBuffer (which is a tad slower).
bye, Ralf
 
V

Vera

My bad, I should've specified: sum is a double and the vector "tokens"
stores double values.

The full compiler message is: "operator + cannot be applied to
double,java.lang.Object"
 
P

Patricia Shanahan

Vera said:
My bad, I should've specified: sum is a double and the vector "tokens"
stores double values.

The full compiler message is: "operator + cannot be applied to
double,java.lang.Object"

No Java Vector can contain anything other than references to objects,
but autoboxing will do some automatic object creation which may be
confusing matters, making it look as though you can add a double.

How was tokens declared? How is it initialized?

Patricia
 
V

Vera

This is what I have:
--------------------------------------------------------------------
Vector tokens = new Vector();

try
{
// Start reading the file
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);

// Read the file till EOF
while((line = inFile.readLine())!= null)
{
tokenizer = new StringTokenizer(line);

// Convert string to double format
Double lineD = Double.parseDouble(line);

// Print number
// System.out.println(lineD);

// Store number in array
tokens.add(lineD);
}
----------------------------------------------------------------------------------
 
P

Patricia Shanahan

Vera said:
This is what I have:
--------------------------------------------------------------------
Vector tokens = new Vector();

try
{
// Start reading the file
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);

// Read the file till EOF
while((line = inFile.readLine())!= null)
{
tokenizer = new StringTokenizer(line);

// Convert string to double format
Double lineD = Double.parseDouble(line);

// Print number
// System.out.println(lineD);

// Store number in array
tokens.add(lineD);
}

tokens contains references to Double objects.

There are two paths from here to where you want to be. I'm going to
discuss the older path, rather than opening the generic and autoboxing
cans of worms.

The result of tokens.elementAt(count) is an expression of type Object,
referring to a Double object. You need to do two things to make it
something you can add, change the expression type from Object to Double
with a cast, and invoke a Double method to get the double value:

sum += ((Double)tokens.elementAt(count)).doubleValue();

Patricia
 
V

Vera

Thank you sooooo much! And a special thanks for explaining to me what
was wrong with my code and why you wrote yours the way you did. I'm
very new to this so I don't understand a lot of things, so thank you
for clearing this up for me :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top