Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2007, 09:03 AM
Member
 
Join Date: Nov 2007
Posts: 6
peachyco is on a distinguished road
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).

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: vector[0].addElement(st.nval); 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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-25-2007, 09:11 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
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 09:13 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-25-2007, 10:32 AM
Member
 
Join Date: Nov 2007
Posts: 6
peachyco is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-25-2007, 11:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Code:
vector[0].addElement(Double.valueOf(st.nval));
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-25-2007, 01:57 PM
Member
 
Join Date: Nov 2007
Posts: 6
peachyco is on a distinguished road
Yeah, Double.valueOf() is also working. Thanks for that. But, is there any significant difference between *new Double()* and *Double.valueOf()* ?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-25-2007, 08:07 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply

« help!!! | TicTacToe »

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT: Sorting a vector of object by an element doobybug New To Java 1 03-12-2008 08:37 PM
Unique element in an array revathi17 New To Java 2 12-31-2007 10:44 AM
Max element in an Array mew New To Java 5 12-03-2007 07:26 PM
Performance Issues (adding element to a Vector) JavaForums Java Blogs 0 11-30-2007 08:11 PM
a no such element exception headlice1 New To Java 1 08-07-2007 07:36 PM


All times are GMT +3. The time now is 03:32 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org