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.

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("Enter " + (i + 1) + " Employee data :");

            System.out.print("Enter employee id :");
            emp[i].id = sc.nextInt();
            System.out.print("Enter employee name :");
            emp[i].name = sc.next();
            System.out.print("Enter 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:3
Enter 1 Employee data :
Enter employee id :101
Enter employee name :Xiith-1
Enter employee salary :1000
Enter 2 Employee data :
Enter employee id :102
Enter employee name :Xiith-2
Enter employee salary :2000
Enter 3 Employee data :
Enter employee id :103
Enter employee name :xiith-3
Enter employee salary :3000


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

Employee id  name and salary :101 Xiith-1 1000.0
Employee id  name and salary :101 Xiith-2 2000.0
Employee id  name and salary :101 xiith-3 3000.0