Java Program to print 5 employee details using class


In this program, You will learn how to print 5 employee details using class in Java.


Employee emp[] = new Employee[k];

emp[i] = new Employee();


Example: How to print 5 employee details using class in Java

Run it Now
import java.util.Scanner;

class Employee {
	int id;
	String name;
	float sal;

}

class Main {
	public static void main(String args[]) {

		Scanner sc = new Scanner(System.in);
		System.out.print("Enter How many employee :");
		int k = sc.nextInt();

		Employee emp[] = new Employee[k];

		for (int i = 0; i < k; i++) {
			emp[i] = new Employee();

			System.out.println("\nEnter " + (i + 1) + " Employee data :");

			System.out.print("\nEnter employee id :");
			emp[i].id = sc.nextInt();
			System.out.print("\nEnter employee name :");
			emp[i].name = sc.next();
			System.out.print("\nEnter employee salary :");
			emp[i].sal = sc.nextFloat();

		}

		System.out.println("\n\n============ All employee details are :============\n");

		for (int i = 0; i < k; i++) {
			System.out.println("Employee id  name and salary :" + emp[0].id + " " + emp[i].name + " " + emp[i].sal);

		}
	}

}

Output:

Enter How many employee :2
Enter 1 Employee data :

Enter employee id :101
Enter employee name :John
Enter employee salary :2000
Enter 2 Employee data :

Enter employee id :102
Enter employee name :Mikey
Enter employee salary :3000

============ All employee details are :============

Employee id  name and salary :101 John 2000.0
Employee id  name and salary :102 Mikey 3000.0