Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I need help with Enumeration in hashtable

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default I need help with Enumeration in hashtable

    Hi everybody, i need help with this code:

    import java.util.*;
    public class HashTable {
    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);
    }
    }
    }

    it is supposed to print out the list of names and how many times each entry occurs in the list, for example

    George 3
    Ryan 1

    and so one. The problem is it lists the names but the occurrence count stays zero, it throws a cast error but i don't know where.
    i don't understand why.
    Please help.
    Thank you


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: I need help with Enumeration in hashtable

    if(name == tm.get(j))

    Unless you have good reason otherwise, you should always use the equals method when comparing Strings.

  3. The Following User Says Thank You to copeg For This Useful Post:

    jaouharia1 (November 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: I need help with Enumeration in hashtable

    thank you, i tried it but it still give the same output.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I need help with Enumeration in hashtable

    it throws a cast error but i don't know where.
    The error message gives lots of valuable information, always post error messages with your code and question.

    thank you, i tried it but it still give the same output.
    Any time you make a change to the code and have a question, post the new version of the code so we can see what you see as we communicate.

    Please see the announcements page for the use of code tags when posting code.

Similar Threads

  1. [SOLVED] Question about hashtable
    By leonne in forum Java Theory & Questions
    Replies: 3
    Last Post: November 4th, 2012, 12:09 PM
  2. problem with enumeration
    By exens in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 18th, 2012, 02:51 PM
  3. Need someone to write Hashtable code...
    By realrx7 in forum Paid Java Projects
    Replies: 3
    Last Post: April 23rd, 2012, 09:58 PM
  4. [SOLVED] Hashtable and Key Size
    By sapzero in forum Collections and Generics
    Replies: 0
    Last Post: January 28th, 2010, 02:31 PM