Results 1 to 7 of 7
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Convert Object[] to Double[] using Arrays.copyOfRange

    Hi all,

    I have an array: Object[] row that looks like this:

    [1SWTGKWR, 36N32, A, AR, 15, 0, 29, 30, 12, 86, 34.09, 65.91]

    I want to convert it to an Double[] row2 like this:

    [15, 0, 29, 30, 12, 86, 34.09, 65.91]

    In essence, I want to create a second array of double values that consist of row[4] through row[11].

    I'm initialize double[] row2 thusly:
    Java Code:
    Double[] row2 = new Double[7];
    In attempts to accomplish this, I'm doing
    Java Code:
    row2 = Arrays.copyOfRange(row, 4, row2.length, Double[].class);
    I'm receiving the following error:

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.Arrays.copyOfRange(Unknown Source)
    at PCQ.ChartPanel.getRow(ChartPanel.java:121)
    at PCQ.ChartPanel$1.actionPerformed(ChartPanel.java:9 2)
    at javax.swing.JComboBox.fireActionEvent(Unknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mou seReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMo useEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Where have I gone wrong?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    Have you checked out the API for ArrayStoreException? It states:

    Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
    Perhaps you want to iterate through the array in a for loop and copy the values over to the second array. I'm not sure if you can Arrays.copyOfRange here, but let's see what smarter minds think.

    By the way, what sort of animal is 1SWTGKWR ? It's not a String, not a variable, ... what is it?

    Also, if you iterate through your array and have each item print out what type it is, what do you get? I have a feeling that those numbers may not be numbers.
    Last edited by Fubarable; 04-27-2012 at 01:44 AM.

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    Solved. Java is getting more intuitive as the months pass. I've only been at it since January :p Thanks Furry ;)

    Java Code:
      	double[] row2 = new double[8];
      	int index = 0;
      	for(int i = 4; i < 12; i++){
      		Object object = row[i];
      		String string = object.toString(); double dub = Double.valueOf(string).doubleValue();
      		row2[index] = dub;
      		++index;
      	}
    Edit: I receive the following when I try to up your reputation: "You must spread some Reputation around before giving it to Fubarable again."
    Last edited by Redefine12; 04-27-2012 at 06:56 PM. Reason: posted Double[], meant double[]

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    Quote Originally Posted by Redefine12 View Post
    Edit: I receive the following when I try to up your reputation: "You must spread some Reputation around before giving it to Fubarable again."
    Glad you've got it working. Still, I'm curious as to just what your original array held. And thanks for trying to up my rep, but not to worry.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    If you stores the double values as java.lang.Double in your object array you can just casting it directly into double. No need to convert it to string and then create a double value out of that string.

    Java Code:
    Object[] objects = new Object[] { 10d, 20d };
    
    Double[] doubles = new Double[objects.length];
    doubles[0] = (Double) objects[0];
    doubles[1] = (Double) objects[1];

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    Quote Originally Posted by Redefine12 View Post
    Thanks Furry ;)
    Furry?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Convert Object[] to Double[] using Arrays.copyOfRange

    Quote Originally Posted by Fubarable View Post
    Still, I'm curious as to just what your original array held.
    First array contains stats for Customer Codes in our enterprise. row[0] is the Cust Code. I'm breaking row[4] through row[11] into it's own double[] to serve as input variables for a swing barchart after selection has been performed on row[] via a JComboBox.

Similar Threads

  1. convert integer to double
    By himanwish in forum New To Java
    Replies: 4
    Last Post: 03-17-2011, 02:55 AM
  2. Convert from string to double
    By Lord ice in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 06:27 PM
  3. How to convert a double into a int?
    By tyang in forum New To Java
    Replies: 4
    Last Post: 02-10-2010, 11:02 AM
  4. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 03:46 AM
  5. convert string to a double?
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-27-2007, 04:10 AM

Posting Permissions

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