Zaph0d schrieb:
>> String sum="";
>> for (String s:tokens) {
[quoted text clipped - 3 lines]
> 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.
[quoted text clipped - 7 lines]
> Note2: StringBuilder is not synchronized. If you're multi-threading,
> use StringBuffer (which is a tad slower).
bye, Ralf
Vera - 08 Sep 2006 15:59 GMT
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"
Patricia Shanahan - 08 Sep 2006 16:29 GMT
> 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
Vera - 08 Sep 2006 16:45 GMT
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);
}
----------------------------------------------------------------------------------
Patricia Shanahan - 08 Sep 2006 17:57 GMT
> This is what I have:
> --------------------------------------------------------------------
[quoted text clipped - 21 lines]
> }
> ----------------------------------------------------------------------------------
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
Vera - 08 Sep 2006 20:03 GMT
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 :-)
> tokens contains references to Double objects.
>
[quoted text clipped - 10 lines]
>
> Patricia