Results 1 to 6 of 6
  1. #1
    peachyco is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default Adding a double element to a vector

    Hello, everybody.

    I'm trying to compile this code, but I'm getting an error telling me that the addElement method of java.util.Vector can't be applied to (double).

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Tester
    {
    	
    	//MAIN METHOD
    	public static void main(String[] args)
    	{
    		
    		BufferedInputStream bis = null;
    		double custNum = 0;
    		String custInfo = null;
    		Vector[] vector = new Vector[2];
    		
    		for (int x = 0; x < vector.length; x++)
    			vector[x] = new Vector(10,5);
    		
    		try
    		{
    			bis = new BufferedInputStream(new FileInputStream("customer_records.txt"));
    			StreamTokenizer st = new StreamTokenizer(bis);
    			
    			//Java Tokenizing Declarations
    			st.wordChars('_', '_');
    			st.eolIsSignificant(true);
    			st.ordinaryChars(0, ' ');
    			
    			int token = st.nextToken();
    			while (token != StreamTokenizer.TT_EOF)
    			{
    				token = st.nextToken();
    				switch (token)
    				{
    					case StreamTokenizer.TT_NUMBER:
    						[B][I][COLOR="DarkRed"]vector[0].addElement(st.nval);[/COLOR][/I][/B]
    					
    						if (!custInfo.equals(null))
    							vector[1].addElement(custInfo);
    					
    						custInfo = null;
    						break;
    						
    					default:
    						custInfo += st.sval + "\n";
    						break;
    						
    				}//end switch
    			}//end while
    			
    			//Print the contents here
    			
    		}//end try
    		
    		catch (IOException ioe)
    		{
    			ioe.printStackTrace();
    			System.exit(1);
    		}//end catch
    		
    		finally
    		{
    			try
    			{
    				if (bis != null) bis.close();
    			}//end try
    			
    			catch (IOException ioe)
    			{
    				ioe.printStackTrace();
    				System.exit(1);
    			}//end catch
    			
    		}//end finally
    		
    	}//end main
    	
    }//end class Tester
    I feel I'm just missing sumthing pretty obvious, but for the n00b I am, I can't see it! Please help. Thanx a lot in advance!

  2. #2
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    Vector <Type> Name = new Vector <Type> ();
    why you are using a vector by the way? it is out of fashion. use arraylist
    Last edited by Zensai; 11-25-2007 at 07:13 AM.

  3. #3
    peachyco is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    Thanks anyway for the help.

    I'm using vector right now 'coz that's what we're told to use.

    Anyways, I found the problem out. A vector can't work with primitives, just like ArrayLists. I had to wrap the double up before appending it to the vector.

    Java Code:
    vector[0].addElement(new Double(st.nval));
    I'm having more problems now that I've finished compiling and am actually running the program, but that's for another thread. ;)

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Vector can take an Object such as Double but not a primitive data type such as double. So wrap your double value in a Double.
    Java Code:
    vector[0].addElement(Double.valueOf(st.nval));

  5. #5
    peachyco is offline Member
    Join Date
    Nov 2007
    Posts
    10
    Rep Power
    0

    Default

    Yeah, Double.valueOf() is also working. Thanks for that. But, is there any significant difference between *new Double()* and *Double.valueOf()* ?

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    is there any significant difference between *new Double()* and *Double.valueOf()*
    The place to resolve these kind of discriminating queries is the javadocs. Look up the constructor and the method you've quoted in the Double class api, follow the links of each to their descriptions in the Method Detail section and you will find a definitive answer.

Similar Threads

  1. URGENT: Sorting a vector of object by an element
    By doobybug in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 06:37 PM
  2. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:44 AM
  3. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM
  4. a no such element exception
    By headlice1 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •