如何在java中的现有数组中添加字符串用户输入?

时间:2016-03-28 08:03:10

标签: java arrays string

我正在尝试显示员工完成的认证。在一个数组中说“证书”我持有在创建员工时输入的证书。现在,我希望我的员工通过将证书添加到同一阵列来更新他的“证书”。我希望在更新后显示员工完成的总认证列表。这是我的代码位

{
  private String FirstName, LastName;

  protected float salary;

  private char grade;

  private int empid;

  private static int i;

  private int j;

  private Date1 dt; //*//

  private int experience;

  private String designation;

  final String var1 = "YES";

  String var2;

  Scanner enterCertificate = new Scanner(System.in);

  private String[] certificate = new String[]{};

// CONSTRUCT

public Employee(String nmf, String nml, float sal, char gd, Date1 dt, int experience, String designation, String[] certificate)
{
  FirstName = nmf;
  LastName = nml;
  salary = sal;
  this.dt = dt; //*//
  grade = gd;
  this.experience = experience;
  this.designation = designation;
  this.certificate = certificate;
  empid = ++i;
}


// DISPLAY

public void display()
{
  System.out.println("Employee"+empid+ " Complete Details : ");
  System.out.print('\n');
  System.out.println("Employee FirstName is " + FirstName);
  System.out.println("Employee LastName is " + LastName);
  System.out.println("Employee Salary is Rs." + salary);
  System.out.println("Employee Grade is " + grade);
  System.out.print("Joining Date is ");
  dt.DateDisplay();
  System.out.println("Employee experience is " + experience + " Years");
  System.out.println("Employee Designation is " + designation);
}


public String[] updateCertificate() 
{
    System.out.println("Do you want to update certification ? YES/NO");
    Scanner scanner3 = new Scanner(System.in);
    var2 = scanner3.nextLine();
    if(var1.equalsIgnoreCase(var2))
    {
        System.out.println("Enter Certification Details for any Updates: ");
        this.certificate[j] = enterCertificate.nextLine();
    }
    else
    {
        return null;
    }
    return certificate;
}

public String toString()
{
    return FirstName + ", " + "Employee ID: " + empid + ", " + experience + " Years Experience"+ ", " + designation + ", " + "Certification: " + certificate[j]+ ".";
}

// ID DISPLAY

public static void count()
{
 System.out.println("Total number of employees is " + i);
}


@Override
public String[] displayUpdateCertificate() 
{
    String[] displaycertificate = new String[5];
    displaycertificate = certificate;
    if(var1.equalsIgnoreCase(var2))
    {
        System.out.println("Displaying updated certificate List.....");
        System.out.println(Arrays.toString(certificate));
    }
    else
    {
        System.out.println("No Certificates were updated....");
    }
    return displaycertificate;
}

} 

我的主要课程就是这样......

    public static void main(String[] args)
  {
   Date1 dt = new Date1(12,11,2015); //*//
   Employee emp1,emp2,emp3,emp4,emp5;
   emp1 = new ProjectManager("MaheshKumar","Siddaraju",16700,'A',dt, 7, "Project Manager",new String[] {"PMI"});   // To input array as parameter new String[] form is used
   ((ProjectManager)emp1).ProjectManagerDisplay();                                                                                    //Downcasting
   emp2 = new ContractTechnicalAssociate("Mohammad","Javeed",0,'A',dt, 5, "Contract based Technical Associate",new String[] {"SCJP"});
   ((ContractTechnicalAssociate)emp2).ContractTechnicalAssociateDisplay();
   emp3 = new TechnicalAssociate("Aruna","Daggubati",16700,'A',dt, 6, "Technical Associate",new String[] {"SCJP"});
   ((TechnicalAssociate)emp3).TechnicalAssociateDisplay();
   emp4 = new ProjectManager("Kiran","Vadlamudi",16700,'A',dt, 4, "Project Manager",new String[] {"PMI"});
   ((ProjectManager)emp4).ProjectManagerDisplay();
   emp5 = new TechnicalAssociate("Neenu","Sebastian",16700,'A',dt, 9, "TechnicalAssociate",new String[] {" "});
   ((TechnicalAssociate)emp5).TechnicalAssociateDisplay();
   Employee.count();
   scanner = new Scanner(System.in);
   int i;
   while(true)
   {   
   System.out.println("Enter EmployeeID to get Employee short details : ___ ");
   i = scanner.nextInt();
   System.out.println("Employee ID entered is: " + i);
   System.out.println("Fetching Employee Details......" + '\n');`

所以我想用主类

添加更新的证书

1 个答案:

答案 0 :(得分:0)

您应该使用ArrayList或make arraycopy到长度为+ 1的新数组。