0

I am trying to write a program to check the salaries of 2 employees. So I first accept the name of all the employees in an array named employeeName and sales by the employees in the array named AnnualSales. I am facing problem when I am try to check a user entered name with a name stored in the employeeName array.

import java.util.Scanner;

public class Array {

    public static void main(String[] args) {

        int numberOfEmployees;                              // will store the number of employees
        int compare;                                        // number of employees you want to compare results to

        //Scanner class enables user input
        Scanner sp = new Scanner(System.in);

        System.out.println("Enter the number of employees: ");
        numberOfEmployees = sp.nextInt();

        String[] employeeName = new String[numberOfEmployees];              // this string array will store the name of employees
        double[] AnnualSales = new double[numberOfEmployees];               // this will store the sales of every individual employee

This will store all the employeeNames and their AnnualSales.

        for(int i = 0 ; i < numberOfEmployees ; i++) {

            System.out.printf("Enter name of employee %d: ",i+1);
            employeeName[i] = sp.next();

            System.out.printf("Enter salary of employee %d: ",i+1);
            AnnualSales[i] = sp.nextDouble();

        }

        System.out.println("Enter the number of employees you want to compare records of: ");
        compare = sp.nextInt();

        if(compare > numberOfEmployees) {

            System.out.println("IndexOutOfBound");
            System.exit(0);
        }

        String[] comparison = new String[compare];

        for(int i = 0; i < compare; i++) {

            System.out.printf("Enter name of employee %d for comparison: ",i+1);
            comparison[i] = sp.next();

            // a loop to go through all the names in the employeeName array
            System.out.println(comparison[i]);

I just want to check whether the name of the employee is already in the employeeName array. The below if condition exits on comparing the 1st name in the employeeName array but I want to check the name of a particular employee with all the employees in the employeeName array.

            for(int j = 0 ; j < numberOfEmployees ; j++) {  
                if(comparison[i] != employeeName[j]) {
                    System.out.println("Employee does not exist!");
                    System.exit(0);
                }
            }
        }

        // compare salary of 2 employees

        if(AnnualSales[compare-1] > AnnualSales[compare-2]) {

            System.out.printf("Sales of %s are greater than %s",employeeName[compare-1], employeeName[compare-2]);
        }else {
            System.out.printf("Sales of %s are less than %s",employeeName[compare-1], employeeName[compare-2]);
        }

        sp.close();
    }
}
Abhijith S
  • 526
  • 5
  • 17
Lakshya Munjal
  • 77
  • 2
  • 10

2 Answers2

0

Don't compare string with == like this:

comparison[i] != employeeName[j]

Using equals() instead:

!comparison[i].equals(employeeName[j])
Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
  • 2
    You marked the previous question as a dupe (https://stackoverflow.com/questions/50543239/java-getheaderfields-header-is-not-equal-to-its-value) but yet answered this :-| – achAmháin May 26 '18 at 13:40
  • This one is hidden because he's comparing array element. Not straightforward to OP. – Mạnh Quyết Nguyễn May 26 '18 at 13:42
  • @MạnhQuyếtNguyễn i used equals90 method for comparison. Also i did exit the loop when a match is found, and earlier i was exiting the loop when a match is not found. – Lakshya Munjal May 28 '18 at 13:14
0
You need to correct this as:
    for(int j = 0 ; j < numberOfEmployees ; j++) {  
if(!comparison[i].equals(employeeName[j])) {
                        System.out.println("Employee does not exist!");
                        System.exit(0);
                    }
                }
            }
    **Output after changing this condition of your code:**
    Enter the number of employees: 
    3
    Enter name of employee 1: a
    Enter salary of employee 1: 1
    Enter name of employee 2: b
    Enter salary of employee 2: 2
    Enter name of employee 3: c
    Enter salary of employee 3: 3
    Enter the number of employees you want to compare records of: 
    2
    Enter name of employee 1 for comparison: a
    a
    Employee does not exist!
Adya
  • 1,084
  • 9
  • 17