-3

My overall goal is to fill a Hashtable with employee data and be able to access/modify that data then print it out. Given my Employee class here, can I use it like a struct in C? I'm trying to figure out how to initialize a Hashtable and fill it with this data but I'm sort of confused on how to implement it.

public class Employee {

    private String empName;
    private int empNum;
    private String empJob;

    public Employee(String empName, int empNum, String empJob)
    {
        this.empName = empName;
        this.empNum = empNum;
        this.empJob = empJob;

    }

    public int getEmpName()
    {
        return empName;
    }
    public String getEmpNum()
    {
        return empNum;
    }
    public String getEmpJob()
    {
        return empJob;
    }    
}

So, I tried this in main, using String as the key, so I want to use the names as the key so you can search by name. I'm also trying to manually fill it so I can test everything. Also, am I able to access say, the employee number on it's own? if so, how can I do this?

    public class Main {

    public static void main(String[] args) 
    {
        Hashtable<String,Employee> EmployeeTable = new Hashtable<String,Employee>(); 
        Employee Object = new Employee("Donald","Donald", 3, "Engineer");
    }
}

Thanks in advance everyone!

AJ4ms
  • 23
  • 3
  • Also, what exactly is your problem? You created a class, then created an instance in your main and then? What have you tried to do with it, where are your problems, what happened, ....? – UnholySheep Aug 31 '16 at 13:40
  • What do you mean by: `"access the employee number on its own"`? Something like `employeeTable.get("Donald").getEmpNum()`? – charlie Aug 31 '16 at 13:44
  • You should fix your Employee class first and make sure it compiles before anything else. – OH GOD SPIDERS Aug 31 '16 at 13:45
  • Fixed the Employee class on here, I apologize about the typo! – AJ4ms Aug 31 '16 at 13:55

3 Answers3

2

You can add elements to your Hashtable using the put method. You just need to specify the key and the value.

Then you can retrieve values using the get method and specifying the key.

Example Usage:

Hashtable<String, Employee> table = new Hashtable<String, Employee>();
Employee bob = new Employee(...);
table.put("Bob", bob);

Then later you can say...

table.get("Bob");

and this will return Bob's Employee object for you.

Problems with your code:

There are a few problems with your code that you should be aware of.

1. Your Employee Constructor is wrong.

You've got a constructor for Product inside of your Employee class. This is illegal syntax and will not compile (I hope). Instead, you should use the Employee constructor.

2. Your Hashtable variable name matches the Object class.

You've named a variable Object. Object is the class that all java classes inherit from, so you really shouldn't name something this (if it even lets you at all).

The Object documentation mentions this...

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

3. Incorrect Hashtable types.

You've put the wrong types in your Hashtable declaration.

You wrote...

Hashtable<String, Employee> EmployeeTable = new Hashtable<String, Product>();

When really it should be...

Hashtable<String, Employee> employeeTable = new Hashtable<String, Employee>();

(Product changed to Employee)
(I also changed the variable to be lowercase)

Notes:

  • All of the documentation for Hashtable can be found here.

  • You may also be interested in using a HashMap instead of a Hashtable. They're almost identical but HashMap isn't threadsafe. You can see some of the differences here. If you really need a threadsafe map then I'd recommend ConcurrentHashMap, it's up to you to decide which one suits you the best though.

  • It's Java convention for variable names to start with lowercase letters. You don't have to follow this but it's definitely a good idea to. Syntax highlighters will no longer argue with you if you do.

Community
  • 1
  • 1
byxor
  • 5,930
  • 4
  • 27
  • 44
  • 1
    A lot of work, for such a low-quality question. But, well done! Hope you get some more upvotes for this piece of work. – GhostCat Aug 31 '16 at 14:49
  • @GhostCat Thanks for the nice comment, I enjoy seeing positivity on this website. Anything to help out and boost the reputation a little bit haha. – byxor Aug 31 '16 at 14:51
  • 1
    Yeah, and the really bad thing: that guy got -5 on his questions; so worst case; he will delete his question at some point. Puff goes your answer (and mine). Thus, be careful when putting so much effort on answering low quality stuff like this. I am not implying that you better upvote him to protect your work; of course not ;-) – GhostCat Aug 31 '16 at 14:53
  • Don't upvote me, and no I wont be deleting this. All of these answers were great and I whole-hardheartedly appreciate them. I am very new to stackoverflow and getting the hang of Java again. This is a learning process for me. I'll do my best to ask better questions in the future. On a positive note, everyone truly helped out so much and in a digestible way. – AJ4ms Aug 31 '16 at 15:15
1

There are various things wrong with your class.

Example: the class is called Employee. Then the constructor must use that name, and nothing else!

So, it shouldn't read

public Product(String empName, int empNum, String empJob)

but

public Employee(String empName, int empNum, String empJob)

And then your call

Hashtable<String,Employee> EmployeeTable = new Hashtable<String,Product>();

could be correctly written down as

Hashtable<String,Employee> table = new Hashtable<>();

And no, a Hashtable is not a struct. A hashtable is a collection class; in other words: it is a Map. It maps a key (String in your case) to Employee objects.

But, well, stackoverflow is not a service where other people debug and explain your code to you. So, take my input as starting point; and for example: start reading the compiler messages.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

What you want to achieve is rather this:

// Create my Hashtable using the diamond notation indicating  to use the type arguments
// corresponding to the context which is <String, Employee> here
Map<String, Employee> EmployeeTable = new Hashtable<>();
Employee employee = new Employee("Donald", 3, "Engineer");
// put my employee into my map using empoyee's name as key
EmployeeTable.put(employee.getEmpName(), employee);

What you are looking for is Map#put(key, value)

After fixing several typo issues, your class Employee should be:

public class Employee {

    ...
    public Employee(String empName, int empNum, String empJob)
    {
        ...
    }

    public String getEmpName()
    {
        return empName;
    }
    public int getEmpNum()
    {
        return empNum;
    }
    ...
}

NB: Hashtable is an outdated class, you should not use it anymore, if you don't intend to share it use an HashMap instead and if you want to share it use a ConcurrentHashMap

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122