File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ClassCastException " Watch "ClassCastException " New topic
Author

ClassCastException

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 489

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


"If the facts don't fit the theory, get new facts" --Albert Einstein
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

And the problem?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Seetharaman Venkatasamy
Bartender

Joined: Jan 28, 2008
Posts: 4503

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


Not everything that counts can be counted, and not everything that can be counted counts-Albert Einstein
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11642

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18365

And iterate over the entrySet() instead of iterating over the keySet(), then retrieving the elements using get:


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
subject: ClassCastException
 
Threads others viewed
Array of Hashtables :-How to display
significance of private with final modifier method
WSDL Validation
Iteration
encoding attribute
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture