Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Eclipse ordering and then sorting 1 array, maybe more?

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");  
      }
}
}
like image 477
Ryan Horner Avatar asked Jul 30 '26 03:07

Ryan Horner


1 Answers

A quick but bad performance patch to your code below:

// 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

}

//patch starts here
int[] totalHoursSortedAsc = Arrays.copyOf(totalHours, totalHours.length);
Arrays.sort(totalHoursSortedAsc);
for (int i = totalHoursSortedAsc.length - 1;i>=0;i--) {
    for (int j = 0;j < totalHours.length;j++) {
        if (totalHoursSortedAsc[i] == totalHours[j]) {
            System.out.println("Employee " + (j + 1) + " worked " + totalHours[j] + " hours");
            totalHours[j] = -1; //Employees may work the same time
        }
    }
}

A better way will be sorting your EmployeeWorkHours by Collections.sort() as Andreas suggested. Look here for the example code.

like image 188
free6om Avatar answered Aug 01 '26 17:08

free6om



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!