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?