0
import java.util.Scanner;
import java.util.Vector;
import java.util.Enumeration;

class Emp
{
    private String id;
    private String name;
    private char gender;
    private String title;
    private String org;

    public Emp(String id, String name, char gender, String title, String org)
    {   
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.title = title;
        this.org = org;
    }

};

public class program2 {
    public static void main(String [] arg){
        Scanner sc = new Scanner(System.in);
        System.out.println("\n-----EMPLOYEE DATABASE-----");
        System.out.println("\n1. Add Employee");
        System.out.println("\n2. Delete Employee");
        System.out.println("\n3. Display Details");
        System.out.println("\n4. Compare Salaries");
        System.out.println("\n5. Search Employee");
        System.out.println("\n6. Sort Data");
        System.out.println("\n7. Exit");

        Vector <Emp> vec = new Vector<Emp>();
        while (true) 
        {
            try
            {
                System.out.print("\nEnter choice: ");
                int choice = Integer.parseInt(sc.nextLine());
            
            switch (choice) 
            {
                case 1: System.out.println("\n---------------------");
                        System.out.print("\nEmployee ID: ");
                        String id = sc.nextLine();
                        System.out.print("Employee Name: ");
                        String name = sc.nextLine();
                        System.out.print("Employee Gender (M/F): ");
                        char gender = sc.nextLine().charAt(0);
                        System.out.print("Employee Job Title: ");
                        String title = sc.nextLine();
                        System.out.print("Employee Organisation: ");
                        String org = sc.nextLine();

                        vec.addElement(new Emp(id, name, gender, title, org));
                        System.out.println("\nEmployee Details Added!");
                        System.out.println("\n---------------------");
                        break;

                case 2: 
                        break;

                case 3: Enumeration vEnum = vec.elements();          //Using enum variable to display vector elements
                        while(vEnum.hasMoreElements())
                        {
                            System.out.print(vEnum.nextElement()+"\t");
                        }
                        break;

                case 7: System.exit(0);

                default: System.out.println("\nInvalid Input!");
            }
            }
            catch (Exception e)
            {
                System.out.println("\nInvalid Input!");
            }
    
        }   
    }
}

Please Help! The program complies perfect but it is returning some garbage value (EMP@123AE..) when trying to display vector elements. I am new to Java and I have to use Vectors as this is part of my college assignment. Also, is there any other method (without using enumeration) to print objects in the Vector?

Jim Jonas
  • 5
  • 1
  • 3
  • 2
    Does this answer your question? [How to use the toString method in Java?](https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) Hint: Override method `toString()` in class `Emp`. – Abra Feb 20 '21 at 11:38
  • Override the toString method in Emp so that will print a readable value instead of the object default 'name' – Tokazio Feb 20 '21 at 11:39
  • If you look at the documentation for Vector, just using, `System.out.println(vec)` will print the string representation of each element. So once you create you're `toString` for your `Emp` class, you will not need to use the enumeration. – matt Feb 20 '21 at 11:49
  • Thankyou! Also can you help me with the 'Delete Employee' method? @Abra – Jim Jonas Feb 20 '21 at 11:59
  • I just need an approach to implement the Delete method. I want to ask the user for the EmpID and if that exists in the record, it is deleted. How can I target the id in the object and delete it using `vec.remove()`? @Abra – Jim Jonas Feb 20 '21 at 12:27

0 Answers0