File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I'm working on creating a symbol table for a parser I am writing and I am using a hashtable to implement my symbol table. I'm running into some trouble when I try to print out the values in my table. Here's what I'm doing:


here is my driver:



Any help would be great.
Thanks,
Hunter

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64171
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the problem?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hunter McMillen wrote:



element = (String)iterator.nextElement(); it gives you a value element of Hashtable which is SymbolTableEntry . SymbolTableEntry is not a String , but you are trying to cast to String, so Obviously you get *ClassCastException*

hth
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that classes Hashtable and Enumeration are legacy collection classes. They have been (more or less) replaced by the collections framework in Java 1.2 (a long time ago!). See Sun's tutorial about collections for more information.

Use HashMap instead of Hashtable and Iterator instead of Enumeration.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And iterate over the entrySet() instead of iterating over the keySet(), then retrieving the elements using get:
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic