Question

Computing the weekly hours for each employee:

Suppose the weekly hours for employees are stored in an array. It records the employee's seven-day work hours with seven elements. For example, we store the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours. Example input:

               S  M  Tu W  Th F  S

Employee1-Paul 0  8  8  8  8  8  0
Employee2-Mary 4  8  8  5  8  0  2
Employee3-Yin  5  8  8  2  8  0  1

Print totals:

Paul: 40 hours.
Mary: 35 hours.
Yin: 32 hours.

This is the question that for my assignment but i'm getting 2 logical errors. Heres my code.

import java.util.*;

    public class employeeHours {
    public static void main(String[] arges) {
        Scanner turtle = new Scanner(System.in);
        String[] calender = { "S", "M", "T", "W", "Th", "F", "S" };
        System.out.println("How many Employee's do you have?: ");
        int NUMBER_OF_EMPLOYEES = turtle.nextInt();
            turtle.nextLine();
        int [][]hours;
        hours = new int[NUMBER_OF_EMPLOYEES][7];
        String[][] employee = new String[NUMBER_OF_EMPLOYEES][2];
        // input for Names
        for (int x = 0; x < (employee.length); x++) {
            System.out.println("Name of Employee " + (x + 1) + ": ");
            String name = turtle.nextLine();
            employee[x][1] = name;

        }
        // input for Hours
        for (int z = 0; z < hours.length; z++) {
            System.out.println("Starting from Sunday Enter the hours Employee "+ (z + 1)+ " have worked (Make sure you seperate it by spaces): ");
            for (int a = 0; z < (hours[0].length); a++) {
                hours[z][a] = turtle.nextInt();
            }
        }
        // Print everything out
        for (int i = 0; i < employee.length; i++) {
            for (int z = 0; i <= employee[0].length; z++) {
                System.out.print(employee[i][z] + "-");
            }
            for (int f = 0; f < NUMBER_OF_EMPLOYEES; f++) {

                System.out.print(" " + hours[i][f]);

            }
        }
        // Total hours.
        for (int s = 0; s < hours[0].length; s++) {
            int counter = 0;
            for (int d = 0; d < hours.length; d++) {
                hours[d][s] += counter;
            }
            System.out.println("Employee " + (s + 1) + ":" + counter + " Hours");
        }

    }

}

Alright when i run this code. Its going to ask me how many employees there are. So i put in 2 as an example. In it's suppose to ask the name of employee number 1, i enter name, then outputs enter name of employee number 2. Instead of doing this it spits out "Enter Employee 1's Name" and "Enter Employee 2's Name" at the same time.

see!

How many Employee's do you have?:
2
Name of Employee 1:
Name of Employee 2:

OK Now i'm getting another error with java.lang.ArrayIndexOutofBoundsexception.

How many Employee's do you have?:
2
Name of Employee 1:
marry
Name of Employee 2:
paul Starting from Sunday Enter the hours Employee 1 have worked (Make sure you seperate it by spaces):
1
1
1
1
1
1
1
1
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at employeeHours.main(employeeHours.java:24) ????

Was it helpful?

Solution

There is one problem when using nextInt().

If you use it on classic user-input, it takes integer, but it left end of line in that scanner. So when you use nextLine, it finds the end of line and "skip it".

The easiest solution is to "flush it" after use:

    int NUMBER_OF_EMPLOYEES = turtle.nextInt();
    turtle.nextLine();

Then you mispelled the right variable, change this line :

for (int a = 0; z < (hours[0].length); a++) {

to this :

for (int a = 0; a < (hours[0].length); a++) {


and the last mistake

This line

for (int z = 0; i <= employee[0].length; z++) {

should be changed to :

for (int z = 0; z < employee[0].length; z++) {

Runnable code :

public static void main(String[] arges) {
    Scanner turtle = new Scanner(System.in);
    String[] calender = { "S", "M", "T", "W", "Th", "F", "S" };
    System.out.println("How many Employee's do you have?: ");
    int NUMBER_OF_EMPLOYEES = turtle.nextInt();
    turtle.nextLine();
    int [][]hours;
    hours = new int[NUMBER_OF_EMPLOYEES][7];
    String[][] employee = new String[NUMBER_OF_EMPLOYEES][2];
    // input for Names
    for (int x = 0; x < (employee.length); x++) {
        System.out.println("Name of Employee " + (x + 1) + ": ");
        String name = turtle.nextLine();
        employee[x][1] = name;

    }
    // input for Hours
    for (int z = 0; z < hours.length; z++) {
        System.out.println("Starting from Sunday Enter the hours Employee "+ (z + 1)+ " have worked (Make sure you seperate it by spaces): ");
        for (int a = 0; a < (hours[0].length); a++) {
            hours[z][a] = turtle.nextInt();
        }
    }
    // Print everything out
    for (int i = 0; i < employee.length; i++) {
        for (int z = 0; z < employee[0].length; z++) {
            System.out.print(employee[i][z] + "-");
        }
        for (int f = 0; f < NUMBER_OF_EMPLOYEES; f++) {

            System.out.print(" " + hours[i][f]);

        }
    }
    // Total hours.
    for (int s = 0; s < hours[0].length; s++) {
        int counter = 0;
        for (int d = 0; d < hours.length; d++) {
            hours[d][s] += counter;
        }
        System.out.println("Employee " + (s + 1) + ":" + counter + " Hours");
    }

}

Sample output :

How many Employee's do you have?: 
1
Name of Employee 1: 
libik
Starting from Sunday Enter the hours Employee 1 have worked (Make sure you seperate it by spaces): 
10 20 30 40 50 60 70
null-libik- 10Employee 1:0 Hours
Employee 2:0 Hours
Employee 3:0 Hours
Employee 4:0 Hours
Employee 5:0 Hours
Employee 6:0 Hours
Employee 7:0 Hours
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top