File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ArrayList.get - cast to String problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "ArrayList.get - cast to String problem" Watch "ArrayList.get - cast to String problem" New topic
Author

ArrayList.get - cast to String problem

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 788
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
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Looks like you ArrayList "elems" actually contain arrays of Strings, not just Strings.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 788
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;
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7

// 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.


Android appsImageJ pluginsJava web charts
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
(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

Joined: Apr 09, 2002
Posts: 788
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList.get - cast to String problem
 
Similar Threads
JFreeChart - sort by values
who can tell what is the method parseInt( )?
ArrayList.get - cast to String problem
Comparator and Sorting Arrays
SCJP Brainteaser (4)