Question:

Write a program to accept the designations of 100 employees in a single dimensional array. Accept the designation from the user and print the total number of employees with the designation given by the user as input.

Show Hint

When comparing strings in Java, always use \texttt{equals()} or \texttt{equalsIgnoreCase()} instead of \(==\), because \(==\) compares references, not actual string values.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Understand the requirement of the question.
In this question, we have to store the designations of \(100\) employees in a one-dimensional array. After storing all the designations, we need to accept one designation from the user and count how many times that designation appears in the array. Finally, we print that total count.

Step 2: Declare the required variables and array.
We need a string array to store the designations of \(100\) employees. We also need one variable to store the designation entered by the user for searching, and one counter variable to count the number of matches.

Step 3: Accept the designations of 100 employees.
Using a loop, we input all \(100\) designations one by one and store them in the array. Since designations are words such as Manager, Trainee, Director, etc., we use a String array.

Step 4: Search the given designation in the array.
After taking the input designation to be searched, we traverse the array again. Every time the entered designation matches an element of the array, we increase the counter by \(1\). For string comparison in Java, we use the equalsIgnoreCase() method so that the comparison works properly even if the user enters the designation in a different letter case.

Step 5: Display the final result.
At the end of the loop, the counter contains the total number of employees having the required designation. This value is then printed as the output.

Step 6: Write the complete Java program.

import java.util.Scanner;

class EmployeeDesignation
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        String desig[] = new String[100];
        String search;
        int i, count = 0;

        for(i = 0; i < 100; i++)
        {
            System.out.print("Enter designation of employee " + (i + 1) + ": ");
            desig[i] = sc.nextLine();
        }

        System.out.print("Enter designation to be searched: ");
        search = sc.nextLine();

        for(i = 0; i < 100; i++)
        {
            if(desig[i].equalsIgnoreCase(search))
            {
                count++;
            }
        }

        System.out.println("Total number of employees = " + count);
    }
}

Step 7: Explain the example given in the question.
For example, if the designations stored are Trainee, Manager, Chef, Manager, Director, Manager and the input designation is Manager, then the word Manager appears \(3\) times. Therefore, the output will be \(3\).

Was this answer helpful?
0
0

Questions Asked in ICSE Class X board exam

View More Questions