944,155 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 407
  • Java RSS
Nov 11th, 2009
0

Conversion error - I'm completely stumped!

Expand Post »
The line I get an error on is this:
Java Syntax (Toggle Plain Text)
  1. 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?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 11th, 2009
0
Re: Conversion error - I'm completely stumped!
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?
Last edited by masijade; Nov 11th, 2009 at 7:45 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 11th, 2009
0
Re: Conversion error - I'm completely stumped!
the entire piece of code looks like this:
Java Syntax (Toggle Plain Text)
  1. TreeMap tm = new TreeMap();
  2.  
  3.  
  4. try {
  5. FileInputStream fiStream = new FileInputStream("C:\\Work\\Tracker\\TEST.txt");
  6. DataInputStream diStream = new DataInputStream(fiStream);
  7. BufferedReader bReader = new BufferedReader(new InputStreamReader(diStream));
  8. String inLine;
  9.  
  10. while ((inLine = bReader.readLine()) != null) {
  11. // Row gets inserted into the Treemap as the key, with the account number as the value
  12. String [] record = inLine.split("\\|", -1); // Splits the record, keeps empty fields.
  13.  
  14. String accNo = record[0];
  15. String restOfLine = "";
  16.  
  17. for (int i = 1; i < record.length; i++) {
  18. restOfLine = restOfLine + "," + record[i];
  19. }
  20.  
  21. if (!record[0].equals("ACCOUNT NUMBER")) {
  22. tm.put(accNo, restOfLine);
  23. }
  24. //type mismatch: cannot convert from Object to List
  25. List accDetails = (List)tm.get(accNo);
  26. if (accDetails == null) {
  27. accDetails = new ArrayList();
  28. }
  29. accDetails.add(restOfLine);
  30.  
  31. tm.put(accNo, accDetails);
  32. }
  33. 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
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 11th, 2009
0
Re: Conversion error - I'm completely stumped!
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 11th, 2009
0
Re: Conversion error - I'm completely stumped!
Thanks!

Solved my problem, and gave me a better idea as well
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help inserting array into Method
Next Thread in Java Forum Timeline: adding more then one element to a line in a JList





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC