I need to display the values entered by the user in the Names array and the Salary array. However, I only need to display the Employee name and Salary if it is over the average. The problem I am having is that it runs the program fine but doesn't compare Salary to Average.
import java.util.Scanner;
import java.text.DecimalFormat;
public class Salaries
{
public static void main(String [] args)
{
int numEmployees; //number of employees
String[] Names; //array to store names
int[] Salary; //array to store salaries
double Average=0.0; //Average of Salary
//Creates new scanner object
Scanner keyboard = new Scanner(System.in);
//Create decimal format object
DecimalFormat formatter = new DecimalFormat("#0.00");
//User enteres numbers of employees to size array
System.out.print("How many employees will you be entering? ");
numEmployees = keyboard.nextInt();
//Create the size of the arrays for Names and Salary
Names = new String[numEmployees];
Salary = new int[numEmployees];
//for loop to run the array filler
for (int i = 0; i < numEmployees; i++)
{
System.out.print("Enter employee #" + (i + 1) + "'s name: ");
Names[i] = keyboard.next(); //gets the name
System.out.print("Enter employee #" + (i + 1) + "'s salary: ");
Salary[i] = keyboard.nextInt(); //gets the salary
//Validation for Salary input
while (Salary[i] < 0)
{
System.out.print("Enter employee #" + (i + 1) + "'s salary: ");
Salary[i] = keyboard.nextInt();
}
//Calculates average
Average = Average + Salary[i];
}
System.out.println("The Average is: " + formatter.format(Average/numEmployees));
//Display Employees and Salary
for (int i = 0; i < numEmployees; i++)
{
if (Salary[i] > Average)
{
System.out.println("Name Salary");
System.out.println(Names[i] + Salary[i]);
}
}
}
}