-3

Could someone try to run this program and help me figure out why it seems really screwed up because not only is there a runtime error but it also is having trouble letting you type in your answer to the prompt. I have no idea what to do or what I've done wrong. I have no compile errors, only issues when I run the program. Specifically a runtime error.Any help would really be appreciated. Thank you!

The program itself is supposed to prompt 10 employees for their

last name

what time they started their work

(The time must entered by hour, then minute in military time. For ex., if they start work at 2:20 the user should enter 14 for the hour and 20 for the minute) `

what time they finished work

(also formatted like start time).

I'm assuming all data is entered correctly, that the employee finished work after they started, and that both times are for the same day.

The program will determine how many hours each employee worked that day.

It will output a well formatted report listing `

all of the employees

along with the amount of time they worked

(listed as hours and minutes),

also it should output

the average amount of time worked

their last name

This is what I've written-

import java.util.Scanner;

public class EmployeeTime 
{
    public static void main(String[] args)
    {

        String[] empLastName={""};
        int[] workStartTime={0}, workEndTime={0}, totalTimeWorked={0}, hoursWorked={0};
        double[] avgHoursWorked={0};
        String[] empLastNameArray= new String[10];
        int[] workStartTimeArray= new int[10];
        int[] workEndTimeArray= new int[10];
        double[] avgHoursWorkedArray= new double[0];
        readEmployeeData(empLastName, workStartTime, workEndTime);
        determineHoursWorked(workStartTime, workEndTime, totalTimeWorked);
        determineAverageHoursWorked(workStartTime, workEndTime, totalTimeWorked);
        writeHeadings();
        printEmployeeInformation(empLastName, workStartTime, workEndTime);
    }

    public static void readEmployeeData(String[] empLastName, int[] workStartTime, int[] workEndTime)
    {

        System.out.println("enter how many students you would like to get the information");


        Scanner input = new Scanner(System.in);
        for(int i=0; i<10; i++)
        {
            System.out.println("Please enter your last name.");
            empLastName[i]=input.next();
            System.out.println("Please enter your work start time.");
            workStartTime[i]=input.nextInt();
            System.out.println("Please enter your work end time.");
            workEndTime[i]=input.nextInt();
        }
    }

    public static int[] determineHoursWorked(int[] workStartTime, int[] workEndTime, int[] totalTimeWorked)
    {
        int[] hoursWorked= new int[10];
        for(int i=0; i<hoursWorked.length; i++)
            hoursWorked[i]= workEndTime[i]-workStartTime[i];
        for(int i=0; i<hoursWorked.length; i++)
        {
            totalTimeWorked[0]=totalTimeWorked[0]+hoursWorked[i];
        }
        return hoursWorked;
    }

    public static double[] determineAverageHoursWorked(int[] workStartTime, int[] workEndTime, int[] totalTimeWorked)
    {
        double[] avgHoursWorked= new double[10];
        for(int i=0; i<avgHoursWorked.length; i++)
            avgHoursWorked[0]= ((totalTimeWorked[0])/10);
        return avgHoursWorked;
    }

    public static void writeHeadings()
    {
        System.out.printf("%s%25s%13s%16s\n\n", "Employees", "Amount of time worked", "Average amount of time worked");
    }

    public static void printEmployeeInformation(String[] empLastName, int[] hoursWorked, int[] avgHoursWorked)
    {
        for(int i=0; i<empLastName.length; i++)
            System.out.printf("%-20s%9d%13.2f%16.2f\n", empLastName[i], hoursWorked[i], avgHoursWorked[i]);
    }
}
Rachel
  • 11
  • 2
  • Can someone also please explain why I got down voted for this question? I'm honestly confused what's wrong with the way I worded this. – Rachel Nov 12 '15 at 06:07
  • 1
    Well, it's a long question, and for example in your code, you wonder why it doesn't work, yet you have lines that look like `String[] empLastNameArray= new String[empLastNameArray.length];` That is a problem because how do you get the length of that variable as you assign it? – OneCricketeer Nov 12 '15 at 06:09
  • 2
    Because questions seeking debugging help ("why isn't this code working?") must include the desired behavior, **a specific problem or error** and the **shortest code necessary** to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Andreas Nov 12 '15 at 06:10
  • @cricket_007 Yes, but that's the reason I'm asking the question. I'm clearly confused and maybe have incorrect code. Just because it's obvious to you doesn't mean it is to me which is why I'm asking. It's long because if it isn't detailed enough people complain and I get down voted. But if it's detailed now that's a problem too? – Rachel Nov 12 '15 at 06:11
  • Okay @Andreas that makes sense. The issue is that I'm so confused I'm not sure how to word it right – Rachel Nov 12 '15 at 06:12
  • @Rachel But you didn't detail. Do you have a compile error? If so, what is the error and on what line? – Andreas Nov 12 '15 at 06:12
  • @andreas sorry, no compile error just runtime error. I was trying to be detailed but maybe in the wrong places. I'm not sure how to explain all of the problems I'm having – Rachel Nov 12 '15 at 06:13
  • 1
    That is your first mistake. One question = one problem. Describe the problem. Don't dump a whole program, and say "I have 10 problems". You have described what the program will/should do, but not what it's doing wrong. – Andreas Nov 12 '15 at 06:15
  • @Andreas okay, let me try to edit this then. – Rachel Nov 12 '15 at 06:17
  • You say you don't have compile errors? The **very first line** doesn't compile: `String[] empLastName="";`. You cannot assign a String to an array. – Andreas Nov 12 '15 at 06:18
  • I was able to compile and run it – Rachel Nov 12 '15 at 06:19
  • 2
    Impossible. That line won't compile. – Andreas Nov 12 '15 at 06:19
  • No, you're completely right. I made adjustments and copied and pasted the wrong one. I will edit – Rachel Nov 12 '15 at 06:21
  • Are you using an IDE to help you develop? Such an Eclipse or IntelliJ? It will show you these errors in your code. – OneCricketeer Nov 12 '15 at 06:22
  • Read and understand [this](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors). – Keale Nov 12 '15 at 06:22
  • @cricket_007 Yes, I'm using eclipse. When I first opened the file it showed the errors to me but when I started working on it again it stopped showing me the errors for whatever reason. I'm not sure if I hit something by mistake – Rachel Nov 12 '15 at 06:23
  • 1
    First two issues: 1) You print "enter how many students", but don't ask for number. 2) When entering data, you get `ArrayIndexOutOfBoundsException: 1` in `readEmployeeData()` (*that* should have been in the question text), which is caused by the fact that the `empLastName[]` was initialized to size 1, and you're trying to assign 10 values. – Andreas Nov 12 '15 at 06:26
  • @Andreas [I know you've seen this behavior already](http://meta.stackoverflow.com/questions/251758/why-is-stack-overflow-so-negative-of-late/252077#252077). :| – Keale Nov 12 '15 at 06:32
  • @Andreas I'm not even sure why I added that print line and I don't think it's necessary so I will probably delete that. Okay, I will try to edit this question and take that into consideration for the next one. Where do I initialize to 1? Is it the line 8 that says `String[] empLastName={""};`? – Rachel Nov 12 '15 at 06:36
  • Correct. It creates an array of size 1 and sets the first (and only) value in the array to an empty string. – Andreas Nov 12 '15 at 06:37
  • Okay, let me try to change that. I will delete the print line as well and see what happens – Rachel Nov 12 '15 at 06:40
  • So for some reason it still says the "enter how many students" line even after I deleted that print line, it still has a runtime error, and it won't let them enter their work end time it just prints the words out and moves on to last name again. – Rachel Nov 12 '15 at 06:44

1 Answers1

0

So... I have no idea how you are supposed to be calculating averages per employee because you only ask for their start and end times once, so the average would be the same amount of hours that they worked.

Anyways, here is a rewrite of the beginnings of what you were trying to accomplish. I hope you learn from it.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Employee {
    String lastName;
    int workStartTime;
    int workEndTime;

    public Employee() { }
}

public class EmployeeTime {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("enter how many students you would like to get the information");
        int numberStudents = input.nextInt();

        List<Employee> employees = readEmployeeData(numberStudents);

        System.out.printf("%s\t%s\t%s\n\n", "Employees", "Amount of time worked", "Average amount of time worked");
        printEmployeeInformation(employees);
    }

    public static List<Employee> readEmployeeData(int numEmployees) {
        Scanner input = new Scanner(System.in);
        List<Employee> employees = new ArrayList<Employee>();

        for(int i=0; i<numEmployees; i++) {
            Employee e = new Employee();

            System.out.printf("[%d] Please enter your last name: ", i);
            e.lastName =input.next();

            System.out.printf("[%d] Please enter your work start time: ", i);
            e.workStartTime=input.nextInt();

            System.out.printf("[%d] Please enter your work end time: ", i);
            e.workEndTime=input.nextInt();

            employees.add(e);
            System.out.println();
        }

        return employees;
    }

    public static int determineHoursWorked(Employee e) {
        return Math.abs(e.workEndTime - e.workStartTime);
    }

    public static void printEmployeeInformation(List<Employee> employees) {
        for(int i=0; i<employees.size(); i++) {
            Employee e = employees.get(i);
            System.out.printf("%-8s\t%21d\t%16.2f\n", e.lastName, determineHoursWorked(e), 0.0);
        }
    }
} 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245