This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ClassCastException " Watch "ClassCastException " New topic


Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

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.

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

Joined: Jan 10, 2002
Posts: 63232

And the problem?

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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*

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14961

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 8 API documentation
Rob Spoor

Joined: Oct 27, 2005
Posts: 20083

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

How To Ask Questions How To Answer Questions
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: ClassCastException
It's not a secret anymore!