Array of Gross Pay for Employee Payroll

I have been tasked with developing a code that calculates an employee(s) gross pay, with the condition that the hourly pay cannot fall below $8. Despite no visible errors during compilation, my code fails to execute.

public static void main(String[] args) {

    Scanner key = new Scanner(System.in);

    int numEmployees = key.nextInt();
    int employeeName[] = new int[numEmployees];
    int hoursWorked[] = new int[numEmployees];
    int hourlyWage[] = new int[numEmployees];
    int grossWages[] = new int[numEmployees];

    System.out.println("Enter the number of employess whose gross wages"
            + " you wish to calculate:");
    //user enter employee name

    for(int i = 1; i < employeeName.length; i++)
    {
        System.out.println("Enter name of employee " + i+ ":");
        employeeName[i]= key.nextInt();
        i++;
    }

    //user enters number of hours
    //System.out.println("How many hours did" + employeeName[i] + " work this week?");

    for(int i = 0; i< numEmployees; i++)
    {
        System.out.print("How many hours did" + employeeName[i] + " work this week?");
        hoursWorked[i] = key.nextInt();

        //get the hourly pay rate
        System.out.print("What is" + employeeName[i] + " hourly wage?");
        //hourlyWage = key.nextInt();

        grossWages[i] = hoursWorked[i] * hourlyWage[i];
    }
    //displays wages
    System.out.println("The hours and pay rates you entered are:");

    for(int i = 0; i < numEmployees; i++)
    {
        //hourlyWage = key.nextInt();
        System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeName[i], hourlyWage);
    }

    //System.out.print("");
    //System.out.print("Name     Hours Worked     Hourly Pay Rate     Gross Wages Earned");
    //System.out.println(employeeName + "     " + hoursWorked + "     " + hourlyWage + "     " + grossWages);

} }

Answer №1

1) Always ask for input before proceeding

System.out.println("Please enter the number of employees for whom you want to calculate gross wages:");

Next, enter the following code:

int numberOfEmployees = key.nextInt();

2) Initialize the loop from zero

for(int i = 0; i < employeeName.length; i++)

3) Remove the comment tags

//hourlyWage = key.nextInt();

Update

Why would an employee's name be represented as an int? Strange.

Answer №2

private static void calculateGrossWages(String[] args) {

Scanner input = new Scanner(System.in);

int numEmployees = input.nextInt();
int employeeID[] = new int[numEmployees];
int hoursWorked[] = new int[numEmployees];
int hourlyWage[] = new int[numEmployees];
int grossWages[] = new int[numEmployees];

System.out.println("Please enter the number of employees you want to calculate gross wages for:");
//User inputs employee names

for(int i = 0; i < numEmployees; i++)
{
    System.out.println("Enter the name of employee " + (i+1) + ":");
    employeeID[i] = input.nextInt();
}

//User inputs hours worked
//System.out.println("How many hours did" + employeeID[i] + " work this week?");

for(int i = 0; i < numEmployees; i++)
{
    System.out.print("How many hours did" + employeeID[i] + " work this week?");
    hoursWorked[i] = input.nextInt();

    //Get the hourly pay rate
    System.out.print("What is" + employeeID[i] + "'s hourly wage?");
    hourlyWage[i] = input.nextInt();

    grossWages[i] = hoursWorked[i] * hourlyWage[i];
}
//Display wages
System.out.println("The hours and pay rates you entered are:");

for(int i = 0; i < numEmployees; i++)
{
    //hourlyWage = input.nextInt();
    System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], hourlyWage[i]);
}

//System.out.print("");
//System.out.print("Name     Hours Worked     Hourly Pay Rate     Gross Wages Earned");
//System.out.println(employeeID + "     " + hoursWorked + "     " + hourlyWage + "     " + grossWages);

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Turn off the scrolling bars and only allow scrolling using the mouse wheel or touch scrolling

Is there a way to only enable scrolling through a webpage using the mouse wheel or touch scrolling on mobile devices, while disabling browser scroll bars? This would allow users to navigate up and down through div elements. Here is the concept: HTML: &l ...

Issue with Three.js failing to display textures

I'm a beginner with three.js and I'm struggling to get my texture to render properly in my scene. Despite following the documentation closely, all I see is a blank canvas with no errors in the console. Can anyone offer any guidance on why my code ...

Struggling to retrieve data from Firebase in React Native?

It's been a challenge for me as a newcomer to React Native trying to retrieve data from a Firebase database. This is the process flow of how my data is handled: 1. A user selects locations and trip details (name, startDate, endDate) --> stored in ...

Is it true that Javascript does not allow for saving or outputting actions?

After coming across this question, I discovered a way to extract a specific element from a Google translate page using Javascript. However, I also learned that it is nearly impossible to directly save something to the clipboard in Javascript without user i ...

The Javascript logic on the NewForm for a Sharepoint 2013 on-premise list is failing to trigger

Screen shot linkThere seems to be an issue with the code I have written. The save button should only be enabled if all 5 checkboxes are ticked, but currently, the button is not disabled on form load. I have tried adding the code in both CEWP and SEWP, bu ...

Can the `lang` attribute be used in a `style` tag to specify the CSS preprocessor language for VueJS? Are there any disadvantages to using this method?

Occasionally, I notice people incorporating code like this: <style lang="scss"> ... </style> <style lang="stylus"> ... </style> I checked the documentation for the style tag and found that lang is not a valid a ...

Tips for filtering data using an array within an object containing arrays

Below is the provided information: userRoom = ['rm1']; data = [{ name: 'building 1', building: [{ room: 'rm1', name: 'Room 1' },{ room: 'rm2', name: ' ...

Node.js and MySQL: Troubles with closing connections - Dealing with asynchronous complexities

I am currently working on a Node program to populate my MySQL database with data from files stored on disk. While the method I'm using seems to be effective, I am facing challenges in ensuring that asynchronous functions complete before ending the con ...

Sorting through a list post a retrieval action

Could you guys please help me understand why my code is not functioning properly? I am receiving an array from my backend rails API, which is providing the data correctly. I have created an empty array where I filter the records based on their ID. The fi ...

Show a webpage depending on specific JavaScript criteria

I have a condition in my JavaScript code that determines whether or not a user should be granted access to a specific page. However, I don't want users to be able to directly access this page even if they know the URL. This page contains both HTML and ...

Exploring the Benefits of Utilizing External APIs in Next JS 13 Server-side Operations

Can someone provide more detail to explain data revalidation in Next JS 13? Please refer to this question on Stack Overflow. I am currently utilizing the new directory features for data fetching in Next JS 13. According to the documentation, it is recomme ...

Utilize Ramda.js to transform commands into a functional programming approach

I have written a code to convert an array of numbers into a new datalist using imperative style. However, I am looking to convert it to functional style using a JavaScript library like ramdajs. Code Background: Let's say we have 5 coins in total with ...

Reveal the class to the global scope in TypeScript

ClassExample.ts: export class ClassExample{ constructor(){} } index.html: <script src="ClassExample.js"></<script> // compiled to es5 <script> var classExample = new ClassExample(); //ClassExample is not defined < ...

Can Node.js handle parsing this as JSON?

Attempting to parse a small API that returns somewhat invalid JSON. Trying the following approach: var request = require('request'), url = 'urlhere'; request({ url: url }, function(error, response, body) { ...

Problems with Bootstrap affix scrolling in Internet Explorer and Firefox

I'm encountering an issue with the sidebar on my website. I've implemented Bootstrap affix to keep it fixed until the end of the page, where it should move up along with the rest of the content at a specific point... Everything seems to be worki ...

Convert an image into a byte array and then into hexadecimal using the 0x? format

Hello everyone, I'm attempting to convert my image file into HEX format similar to what is done on this website. However, the code I am using produces an output like this: The output goes here... I would like the output to be formatted as follows: ...

What is the best way to separate a table column into a distinct column at the specified delimiter count?

I successfully wrote code that splits the third column into new columns at the slash delimiter. However, I am struggling to modify it to split at the nth (i.e. 2nd) occurrence. I couldn't find a solution online, so I'm reaching out here for help ...

Error: The function cannot be called because it is undefined

As a newcomer to JavaScript, I recently copied a script from jqueryui.com for the dialog widget and pasted it into my Yii project. However, upon testing the code, I encountered an error: Uncaught TypeError: undefined is not a function associated with the ...

Transforming an array of flat data into a hierarchical tree structure

I'm facing a challenge with my script. I have an Array of FlatObj and some rules, and I need to create a converter function that transforms them into TreeObj. The Rules are: If an object has a higher depth, it should be a child of an object with a l ...

Guide to retrieving the second value variable in an array based on the selected dropdown option within a controller

In my controller, I am trying to extract the second value in an array list that a user selects from a dropdown so that I can perform mathematical operations on it. $scope.dropdown = [ {name:'Name', value:'123'}] When a user chooses "N ...