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();
}
}