java - hashtables

Discussion in 'OT Technology' started by elevator, Sep 13, 2006.

  1. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    Hey guys...

    I am working on an java programming assignment utilizing a hashtable. The instructions:

    Write a Java application:
    -Using a Hashtable, read in (console input) and store some employees (each employee has a number, name, and department). Then ask the user for an employee’s name and then print out that employee’s data.
    -Create a separate class for an Employee

    So, I'm a newb at hashtables and have no idea how to incorporate the constructor with the hashtable put method or the toString() method in my Employees class with the hashtable get method. I have made the hashtable in the main method.

    My question is once I get the inputs from the console, do I create a new object Employees using the constructor and then put that object in the hashtable? (and if so, how do I put an object into the hashtable? I know how to put individual strings into the hashtable, is it similar?)

    And my newb question is what data type is the key?

    Thanks for the help/hints!
     
  2. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    I'm a newb Java dude also but yeah, whenever you use new to create a new object, the constructor is automatically called.

    So I'd first create the hashtable of Employee type then gather user input, then create object, then hash the object.

    If you know how to create a string hashtable then an Employee hashtable should be easy....just change string to Employee.

    Hope that helps.
     
  3. Dj_MikeyMike

    Dj_MikeyMike New Member

    Joined:
    Jul 30, 2006
    Messages:
    290
    Likes Received:
    0
    Location:
    oZ
  4. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    Yeah, the first place i look is the API on sun's website but I'm note sure about the whole extra class thing...

    my code:
    I'm not even sure if I'm on the right track (I don't even use my toString method :wtc: )
     
  5. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
  6. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    To the OP, you are creating a class Employee (which is another way of saying you are defining a new data type and calling it Employee). Therefore, your hashtable needs to be able to store data of type Employee. So I think you can do something like (my syntax is prolly wrong but I think you'll understand)
    Code:
     
    //gather employee data from the user
     
    Employee e = new Employee(data); //creates a new Employee Object using the data gathered from the user
     
    HashTable<Employee> employees; //creates the hashtable of Employee type
     
    employees.add(e); //adds the Employee object called e to the hashtable
    
    It might take some syntax tweaking to get it to work but it should work.
     
  7. Chau

    Chau if i wanted to kill you, i would've come in here s

    Joined:
    May 30, 2000
    Messages:
    95,448
    Likes Received:
    0
    Location:
    CTU
    create a hashtable/hashmap, create a Employee object for each employee, add each employee to the hashtable with the Employee object as the value, and the employee name as the key.

    then when u ask the user to type in a name, just get the employee from the hashtable by the key (the employee name)
     

Share This Page