| | |
Conversion error - I'm completely stumped!
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 61
Reputation:
Solved Threads: 0
The line I get an error on is this:
"tm" is a TreeMap, and "accNo" is a value in it.
accNo is a String. I get a build error like this:
java.lang.String cannot be cast to java.util.List
I have no idea what to do here, I've tried by myself, and I've googled myself silly, but still no answer...
Can anyone here help?
Java Syntax (Toggle Plain Text)
List accDetails = (List)tm.get(accNo)
"tm" is a TreeMap, and "accNo" is a value in it.
accNo is a String. I get a build error like this:
java.lang.String cannot be cast to java.util.List
I have no idea what to do here, I've tried by myself, and I've googled myself silly, but still no answer...
Can anyone here help?
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
0
#2 18 Days Ago
Seems as though it is Strings stored in the map and that the map was declared using generics.
So, why are you trying to use a String as a List?
IOW, what is in the map and what are you trying to do here?
So, why are you trying to use a String as a List?
IOW, what is in the map and what are you trying to do here?
Last edited by masijade; 18 Days Ago at 7:45 am.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Nov 2009
Posts: 61
Reputation:
Solved Threads: 0
0
#3 18 Days Ago
the entire piece of code looks like this:
so basically, the map holds account numbers and the details associated with them.
I declare that list and assign it a value, and immedialty afterwards I check if it's null or not.
I just want to know how I can get that String into that List
Java Syntax (Toggle Plain Text)
TreeMap tm = new TreeMap(); try { FileInputStream fiStream = new FileInputStream("C:\\Work\\Tracker\\TEST.txt"); DataInputStream diStream = new DataInputStream(fiStream); BufferedReader bReader = new BufferedReader(new InputStreamReader(diStream)); String inLine; while ((inLine = bReader.readLine()) != null) { // Row gets inserted into the Treemap as the key, with the account number as the value String [] record = inLine.split("\\|", -1); // Splits the record, keeps empty fields. String accNo = record[0]; String restOfLine = ""; for (int i = 1; i < record.length; i++) { restOfLine = restOfLine + "," + record[i]; } if (!record[0].equals("ACCOUNT NUMBER")) { tm.put(accNo, restOfLine); } //type mismatch: cannot convert from Object to List List accDetails = (List)tm.get(accNo); if (accDetails == null) { accDetails = new ArrayList(); } accDetails.add(restOfLine); tm.put(accNo, accDetails); } bReader.close();
so basically, the map holds account numbers and the details associated with them.
I declare that list and assign it a value, and immedialty afterwards I check if it's null or not.
I just want to know how I can get that String into that List
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
0
#4 18 Days Ago
Well, what is stored in the Map is a String, you cannot simply cast a String to a List. Do you want to create a List from multiple elements in this String, or do you want to simply add this String (and other's like it) to another list? If the second, you, of course, want to use add. If the first, the use split (if applicable) and create an array, instead.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Help needed for IE6/Netscape problems (Web Browsers)
- Conversion error C2440 (C++)
- Getting a constant conversion error (C++)
- type conversion error (C++)
- fatal error (PHP)
- FILE + conversion from dec to hex = I need help (C)
- conversion to binary (C++)
- Completely stumped by sudden IE slowdown (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Help inserting array into Method
- Next Thread: non-static method in a static context
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






