Conversion error - I'm completely stumped!

Thread Solved

Join Date: Nov 2009
Posts: 61
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training

Conversion error - I'm completely stumped!

 
0
  #1
18 Days Ago
The line I get an error on is this:
  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?
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
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?
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 61
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #3
18 Days Ago
the entire piece of code looks like this:
  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
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 61
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #5
18 Days Ago
Thanks!

Solved my problem, and gave me a better idea as well
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC