8 Replies Latest reply: Nov 2, 2012 10:00 AM by gimbal2 RSS

    I need help with Enumeration in hashtable

    972053
      Hi everybody,
      My code compiles but i don't understand why it throws the error. it says:
      Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
           at java.lang.Integer.compareTo(Unknown Source)
           at java.util.TreeMap.getEntry(Unknown Source)
           at java.util.TreeMap.get(Unknown Source)
           at nku.csc360.homework3.JaouhariAli.ListOfNames.main(ListOfNames.java:35)

      Here is my code, please help!!!

      import java.util.*;

      public class ListOfNames {

           
           public static void main(String...args) {
                
                TreeMap<String, String> tm = new TreeMap<String, String>();
                Hashtable<String, String> ht = new Hashtable<String, String>();
                
                tm.put("1", "George");
                tm.put("2", "Jim");
                tm.put("3", "John");
                tm.put("4", "Blake");
                tm.put("5", "Kevin");
                tm.put("6", "Michael");
                tm.put("7", "George");
                tm.put("8", "Katie");
                tm.put("9", "Kevin");
                tm.put("10", "Michelle");
                tm.put("11", "Ryan");
                tm.put("12", "Michelle");
                tm.put("13", "George");
                
                ht.putAll(tm);
                
                
                Enumeration<String> keys = ht.keys();
                while( keys.hasMoreElements() )
                {
                     String name = keys.nextElement();
                     int count = 0;
                     for (int j = 0; j<tm.size(); j++)
                          if(name == tm.get(j))
                               count++;
                     System.out.println(name + " " + count);
                
                }
                
                
                

      }
      }
        • 1. Re: I need help with Enumeration in hashtable
          rp0428
          Welcome to the forum!
          >
          My code compiles but i don't understand why it throws the error. it says:
          Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
          at java.lang.Integer.compareTo(Unknown Source)
          at java.util.TreeMap.getEntry(Unknown Source)
          at java.util.TreeMap.get(Unknown Source)
          at nku.csc360.homework3.JaouhariAli.ListOfNames.main(ListOfNames.java:35)
          >
          Well you need to use your debugger and step through the code.

          This is the line causing the exception
          if(name == tm.get(j))
          And this is the Javadoc for the TreeMap get method
          http://127.0.0.1:8082/resource/jar%3Afile%3A/C%3A/Software/Java/jdk1.6.0_02/jdk-6-doc.zip%21/docs/api/java/util/TreeMap.html#get(java.lang.Object)

          You are passing 'j' to the method. What is the datatype of 'j'? What is the value of 'j'? Do you have anything in the TreeMap with that value?

          Now what is the value of 'name'? Do you have anything in the TreeMap with that value?

          Use your debugger, or change your code to show what you get if you use
          tm.get(name)
          • 2. Re: I need help with Enumeration in hashtable
            972053
            Thank you for your help. I'm still confused and can't get it to work.
            Here is what i need from the program:
            "Program a way to find the number of times a name occurs in the list. Use a HashTable, the key will be the name, the value will be the number of items it contains."
            I modified my code:

            import java.util.*;

            public class ListOfNames {

                 
                 public static void main(String...args) {
                      
                      TreeMap<String, String> tm = new TreeMap<String, String>();
                      Hashtable<String, String> ht = new Hashtable<String, String>();
                      
                      tm.put("George","1");
                      tm.put("Jim","2");
                      tm.put("John","3");
                      tm.put("Blake","4" );
                      tm.put("Kevin","5");
                      tm.put("Michael","6");
                      tm.put("George","7");
                      tm.put("Katie","8");
                      tm.put("Kevin","9");
                      tm.put("Michelle","10");
                      tm.put("Ryan","11");
                      tm.put("Michelle","12");
                      tm.put("George","13");
                      
                      ht.putAll(tm);
                      
                      
                      Enumeration<String> keys = ht.keys();
                      while( keys.hasMoreElements() )
                      {
                           String name = keys.nextElement();
                           
                           int count = 0;
                           for (int j = 0; j<tm.size(); j++)
                                if(name == tm.get(name))
                                     count++;
                           System.out.println(name + " " + count);
                      
                      }
                      
                 
                      

            }
            }

            but now it displays the names but the count is 0 for all of them:

            Ryan 0
            George 0
            Kevin 0
            Katie 0
            Blake 0
            Michael 0
            Michelle 0
            Jim 0
            John 0



            Thanks in advance.
            • 3. Re: I need help with Enumeration in hashtable
              rp0428
              You didn't respond to any of the questions I ask you in my last reply and now you have posted different code and have different questions.

              I'm not going to do your homework for you.

              You have to stick to one thing until you get it right. You won't be successful if you give up and start over with something new.
              • 4. Re: I need help with Enumeration in hashtable
                EJP
                It also needs to be stated that none of your code bears any relationship to the problem you say you are trying to solve. There is no list, for a start, let alone a list containing names that occur more than once, and no attempt to count them whatsoever. Instead you are starting with a map that already appears to contain what you think the answer might be. Or an ascending sequence number. Or something. I can't figure it out, and clearly you haven't either. You need to stop, take a deep breath, and start again, instead of just writing random code.
                • 5. Re: I need help with Enumeration in hashtable
                  972053
                  I appreciate that, and i apologize, i got frustrated with this exercise. it's not homework, i'm learning java on my own using this book.

                  What is the datatype of 'j'? What is the value of 'j'? Do you have anything in the TreeMap with that value?
                  j is an int, and i'm using it to compare the tree map and he hashtable to count the number of occurences.
                  Now what is the value of 'name'?
                  names is iterating through keys which is the hash table
                  Do you have anything in the TreeMap with that value?
                  no i'm using names to get the values from the hashtable and compare them to the treemap

                  again i apologize for yesterday. and Thank you for your help
                  • 6. Re: I need help with Enumeration in hashtable
                    gimbal2
                    user13784819 wrote:
                    I appreciate that, and i apologize, i got frustrated with this exercise. it's not homework, i'm learning java on my own using this book.
                    Which one? There may be far better ones that can be recommended to you.
                    • 7. Re: I need help with Enumeration in hashtable
                      972053
                      introduction to java programming comprehensive 9th edition

                      but i found the exercise online
                      • 8. Re: I need help with Enumeration in hashtable
                        gimbal2
                        Yeah online exercises... I wouldn't put much stock in them. If you want something useful and accurate I'd try to find more reliable material, like in the SCJP 6 study guide book by Katherine Sierra and Bert Bates. Every chapter ends with plenty of exercises that are meant to prepare you for Java certification. You don't get much more complete than that.

                        Alternatively there is "Head First Java" 2nd edition which teaches you Java through puzzles, games and exercises in stead of through walls of text and code listings.