I have created an Employee class in which I am only passing the value and printing the array. You can see:
package practicequestion;
import java.util.Scanner;
import practicequestion.employeeq.Employee;
public class EmployeeMain {
public static void main(String[] args) {
Employee[] obj = new Employee[3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of inputs you want ");
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
System.out.println("Enter FirstName");
String firstname = sc.nextLine();
System.out.println("Enter LastName");
String lastname = sc.nextLine();
System.out.println("Enter Email");
String email = sc.nextLine();
System.out.println("Enter Salary");
int salary = sc.nextInt();
System.out.println("Enter Mobile Number");
long mobileno = sc.nextLong();
System.out.println("Enter Experience");
int empexp = sc.nextInt();
System.out.println("Enter Employee Id");
int empid = sc.nextInt();
if (i < n) {
obj[i] = new Employee(firstname, lastname, email, salary, empid, empexp, mobileno);
}
}
for (int i = 0; i < 3; i++) {
System.out.println("Employee " + i + " values are: ");
obj[i].Display();
}
if (sc != null) {
sc.close();
}
}
}
But I am not able to input the firstname as whenever I run the program it starts input from last name. Its class is like this:
package practicequestion.employeeq;
public class Employee {
String FirstName;
String LastName;
String Email;
int Salary;
long MobileNo;
int EmployeeExp;
int EmployeeId;
public Employee(String FirstName,String LastName,String Email,int Salary,int EmployeeId,int EmployeeExp,long MobileNo) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Salary = Salary;
this.EmployeeId = EmployeeId;
this.EmployeeExp = EmployeeExp;
this.MobileNo = MobileNo;
}
public void Display() {
System.out.println(FirstName +"\n" + LastName + "\n" + Email + "\n" + Salary + "\n" + EmployeeId +"\n" + EmployeeId +"\n" + MobileNo);
}
}
Can anyone solve this issue?