The following is a Java assignment question and I have most of it completed, but I'm having an issue with displaying the output in descending order (starting from the highest number of hours worked down to the lowest number of hours worked, while also matching those hours with the employee that worked them). I highlighted the part of the problem I need help with in bold and included the sample output.
Problem:
Computing the weekly hours of Employees Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee’s n day work hours with n columns where n ≥ 1 and n ≤ 7 representing the number of days in a week that these employees work. For example, the table shown below represents an array that stores the work hours of eight employees for 7 days in a week. Write a program that takes in as inputs, the number of employees and the number of working days in a week. Then it takes in all the Employee information (name and number of daily hours worked). This program should display employees and their total hours worked in a week in decreasing order of the total hours.
Sample Output:
Employee 7 worked 39 hours
Employee 6 worked 37 hours
Employee 0 worked 34 hours
Employee 4 worked 32 hours
Employee 3 worked 31 hours
Employee 1 worked 28 hours
Employee 5 worked 28 hours
Employee 2 worked 24 hours
Here's what I've got so far, I believe I'm missing something in the last for
Loop but that's why I'm posting this. Any help is very much appreciated:
import java.util.Scanner;
import java.util.Arrays;
public class EmployeeWorkHours {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("How many Employee's do you have?: ");
int NUM_OF_EMPLOYEES = scan.nextInt();
scan.nextLine();
int [][]hours;
int []totalHours= new int[NUM_OF_EMPLOYEES];
hours = new int[NUM_OF_EMPLOYEES][7];
String[] employee = new String[NUM_OF_EMPLOYEES];
// input Names
for (int x = 0; x < (employee.length); x++)
{
System.out.println("Name of Employee " + (x + 1) + ": ");
String name = scan.nextLine();
employee[x] = name;
}
// input Hours
for (int z = 0; z < employee.length; z++)
{
System.out.println("Beginning on Monday, enter the hours Employee "+ (z + 1)+ " has worked each day (Separate each day by spaces): ");
for (int a = 0; a < 7; a++)
{
hours[z][a] = scan.nextInt();
}
scan.nextLine();
}
// Print everything in for loop
for (int i = 0; i < employee.length; i++)
{
totalHours[i]=0;
for(int j=0; j<7; j ++)
{
totalHours[i] = totalHours[i]+hours[i][j];
}
// I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
// Arrays.sort(totalHours); gives me the wrong output, I'm stuck
System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");
}
}
}