2 Replies Latest reply on Nov 27, 2002 1:58 PM by 807593

    Class Cast Exception - TreeSets

    807593
      Hi Everyone,

      I am trying to add a custom object(SymbolTableEntry) to a TreeSet, but I keep getting a runtime error of:

      C:\Java\>java ProcessText
      Exception in thread "main" java.lang.ClassCastException: SymbolTableEntry
      at java.util.TreeMap.compare(TreeMap.java:1081)
      at java.util.TreeMap.put(TreeMap.java:459)
      at java.util.TreeSet.add(TreeSet.java:205)
      at ProcessText.main(ProcessText.java:38)

      Here is my code:

      import javax.swing.*;
      import java.util.*;

      class ProcessText{

      public static void main(String args[]){
      String word = new String();
      String operation = new String();
      String expression = new String();
      String number = new String();
      Integer intNumber;
      int finalNumber = 0;
      Set SymbolTable = new TreeSet();
      Iterator e = SymbolTable.iterator();

      JTextArea outputRegion = new JTextArea(3,5);
      JScrollPane myScroller = new JScrollPane(outputRegion);
      outputRegion.setText("");

      expression = JOptionPane.showInputDialog("Enter your expression.");
      if (!expression.equals("<End>")){
      while (!expression.equals("<End>")){
      StringTokenizer st = new StringTokenizer(expression," ");
      while (st.hasMoreTokens()){
      word = st.nextToken();
      word = word.toLowerCase();
      operation = st.nextToken();
      number = st.nextToken();
      intNumber = Integer.valueOf(number);
      finalNumber = intNumber.intValue();
      //while(e.hasNext()){
      // SymbolTableEntry tempEntry = (SymbolTableEntry)e.next();
      // String tempIdentifier = tempEntry.getIdentifier();
      // if (tempIdentifier.equals(word))
      // tempEntry.performOperation(operation, finalNumber);
      //}
      SymbolTableEntry newEntry = new SymbolTableEntry(word, finalNumber);
      SymbolTable.add((Object)newEntry);

      expression = JOptionPane.showInputDialog("Enter your expression.");
      }
      }

      Can anyone tell me why I'm getting this error?