Results 1 to 6 of 6
- 11-25-2007, 07:03 AM #1
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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).
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!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
- 11-25-2007, 07:11 AM #2
Member
- Join Date
- Nov 2007
- Posts
- 14
- Rep Power
- 0
Vector <Type> Name = new Vector <Type> ();
why you are using a vector by the way? it is out of fashion. use arraylistLast edited by Zensai; 11-25-2007 at 07:13 AM.
- 11-25-2007, 08:32 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
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.
I'm having more problems now that I've finished compiling and am actually running the program, but that's for another thread. ;)Java Code:vector[0].addElement(new Double(st.nval));
- 11-25-2007, 09:25 AM #4
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));
- 11-25-2007, 11:57 AM #5
Member
- Join Date
- Nov 2007
- Posts
- 10
- Rep Power
- 0
Yeah, Double.valueOf() is also working. Thanks for that. But, is there any significant difference between *new Double()* and *Double.valueOf()* ?
- 11-25-2007, 06:07 PM #6
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
-
URGENT: Sorting a vector of object by an element
By doobybug in forum New To JavaReplies: 1Last Post: 03-12-2008, 06:37 PM -
Unique element in an array
By revathi17 in forum New To JavaReplies: 2Last Post: 12-31-2007, 08:44 AM -
Max element in an Array
By mew in forum New To JavaReplies: 5Last Post: 12-03-2007, 05:26 PM -
a no such element exception
By headlice1 in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks