• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList.get - cast to String problem

 
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have the following :
// setObj is a TreeSet and elems and elems2 are ArrayLists (both seem to be set up fine - a.k.a used elsewhere but elems and elems2 are Arrays
for (int i = 0; i <= currItem; i++) {
setObj.add(new Object[] { (String)elems.get(i), new Double(Double.parseDouble(elems2.get(i))) });
}

// With the above I get the following error
The method parseDouble(String) in the type Double is not applicable for the arguments (Object)
// When I try to cast the value inside the parseDOuble method I get the following
java.lang.ClassCastException: [Ljava.lang.String;

How can I use the ArrayLists in this method? Some sort of casting I need to do or something along those lines? Thanks in advance for any help.

Rob
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you ArrayList "elems" actually contain arrays of Strings, not just Strings.
 
Rob Hunter
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the code that generates the elems and elems2. (Going by your response) I'm guessing the way I'm splitting up the string (i.e. strIn) is where the problem lies then. If this is the case how can I break a incoming string up and assign it to an ArrayList in one shot? Thanks for the response.

while ((strIn = dataFile.readLine()) != null) {
//****** Tab-delimited Records ******************************************
readArray.add((Object)strIn.split("\t"));
//****** If more than 1 line then line is a header line ******************
if (readArray.size() > 1) {
if (passData != 0) {
elems.add(currItem, (Object)readArray.get(Integer.parseInt(request.getParameter("PAxis"))));
elems2.add(currItem, (Object)readArray.get(Integer.parseInt(request.getParameter("SAxis"))));
if (request.getParameter("COMPAREPRIM") != null && request.getParameter("COMPARESEC") != null) {
elems3.add(currItem, (Object)readArray.get(Integer.parseInt(request.getParameter("COMPAREPRIM"))));
elems4.add(currItem, (Object)readArray.get(Integer.parseInt(request.getParameter("COMPARESEC"))));
}
currItem++;
}
passData = 1;
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


// When I try to cast the value inside the parseDOuble method I get the following
java.lang.ClassCastException: [Ljava.lang.String;



What kind of objects do the ArrayLists contain? The "[L" part of the error message means that there is an array of String -not just a singular String-, which can't be cast to a double.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Just saw Pauls post and your response to it).

String.split returns a String[], not String. (The cast to Object is not necessary, by the way.)

Are you trying to add all parts of strIn to "elems" individually? Then you might use "elems.addAll(Arrays.asList(strIn.split(...)))", if that doesn't screw up the ordering.
 
Rob Hunter
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf,
Thanks for the response. I just resorted back to using the String[] since I still could in this case. I just wanted to try subbing in ArrayList and try that out. I'll give that a try once I get a chance. Thanks again.

Rob
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
you might use "elems.addAll(Arrays.asList(strIn.split(...)))", if that doesn't screw up the ordering.



It shouldn't screw up the ordering, should it?
 
reply
    Bookmark Topic Watch Topic
  • New Topic