Hello,

I'm writing a program that calculates payroll. I'm using an int array for the employee ID and a for loop for the hours and payrate. I have the code working with one exception. I must write a method for input validation that: Does not accept negative values for hours or numbers less than 6.00 for payrate. Can I do this by adding an if/else statement inside of the for loop? If so, please provide suggestions on how to do it. I've tried it several ways and my code keeps breaking. Thanks.

Here is my code (and I know it looks ugly) but I'm trying.

import java.util.Scanner;

import java.text.DecimalFormat;//Added decimal format for fractional numbers

public class Payroll {

    public static void main(String[] args) {
        
        final int NUM_EMPLOYEES = 7;//number of employees
        double wages;//Hourly pay rate        
              
        //create and initialize an int array
        int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 
                1302850, 7580489};//Employee ID numbers
        
        int[] hours = new int [NUM_EMPLOYEES];
        double[] payRate = new double [NUM_EMPLOYEES];
        
        Scanner keyboard = new Scanner(System.in);     

        //Get each employees hours worked for the week
        System.out.println("Please enter employees " + "hours for this week.");
        
        
        
        for (int index = 0; index < NUM_EMPLOYEES; index++)
        {
        System.out.println("Employee #" + employeeID[index] + ": ");
                hours[index] = keyboard.nextInt();

        while (hours[index] < 0 ) {
                System.out.println("Invalid! Please enter" + "a number greater" +
                            "than 1.");
                hours[index] = keyboard.nextInt();
            }
       
        }
             
            //Get the hourly pay rate
            System.out.println("Enter employee's hourly rate: ");

            
        //Create DecimalFormat object to format numbers
        DecimalFormat dollar = new DecimalFormat("#,##0.00");
                
        {
            
        }
        for (int index = 0; index < NUM_EMPLOYEES; index++)
            
            if (payRate[index] < 7 && hours[index] < 0)
                if (payRate[index] < 0)
                
        {
        while (payRate[index] < 0) {
            System.out.println("Invalid! Please enter " + "an amount greater" +
                            "than 1.");
            payRate[index] = keyboard.nextDouble();
            }
            }
            else
            {
                System.out.println("Employee #" + employeeID[index] + 
                    ":");
                payRate[index] = keyboard.nextDouble();
            }

        //Display each employee's wages
            System.out.println("Wages for each employee:");
            
            for (int index = 0; index < NUM_EMPLOYEES; index++)
            {
                wages = hours[index] * payRate[index];
                System.out.println("Employee #" + employeeID[index] + 
                        ": $" + dollar.format(wages));
            }
        }

        }

Recommended Answers

All 4 Replies

use code tags!!

use code tags!!

Sorry, I'm new to all of this and I didn't know. Thanks for the info.

I have my code all set up, but I keep getting a tag error. I don't know where I messed up at. Can someone take a look at it and let me know?

Thanks.

public class Payroll {

    public static void main(String[] args) {
        
        final int NUM_EMPLOYEES = 7;//number of employees
        double wages;//Hourly pay rate        
              
        //create and initialize an int array
        int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 
                1302850, 7580489};//Employee ID numbers
        
        int[] hours = new int [NUM_EMPLOYEES];
        double[] payRate = new double [NUM_EMPLOYEES];
          
        Scanner keyboard = new Scanner(System.in);}
        {
        //Get each employees hours worked for the week
        System.out.println("Please enter employees " + "hours for this week.");
        
        for (int index = 0; index < NUM_EMPLOYEES; index++)
        {
        do{
        System.out.println("Employee #" + employeeID[index] + ": ");
                hours[index] = keyboard.nextInt();

        while ( hours < 0){
        System.out.println("Invalid! Please enter" + "a number greater" +
                    "than 1.");
        hours[index] = keyboard.nextInt();
        
               
        //Get the hourly pay rate
        System.out.println("Enter employee's hourly rate: ");
                
        //Create DecimalFormat object to format numbers
        DecimalFormat dollar = new DecimalFormat("#,##0.00");
        
        for (int index = 0; index < NUM_EMPLOYEES; index++)
        {
        do{
        System.out.println("Employee #" + employeeID[index] + 
            ":");
        payRate[index] = keyboard.nextDouble();
        
        while(payrate <6.00){
        System.out.println("Invalid! Please enter " + "an amount greater" +
                            "than 5.");
        payRate[index] = keyboard.nextDouble();
        }
 
        //Display each employee's wages
        System.out.println("Wages for each employee:");
            
        for (int index = 0; index < NUM_EMPLOYEES; index++)
            {
                wages = hours[index] * payRate[index];
                System.out.println("Employee #" + employeeID[index] + 
                        ": $" + dollar.format(wages));
            }
            
        }

First of all remove the lines:
do{

They don't do anything. You open the bracket and you don't close it plus you don't have a while to go with it. The while that follows is unrelated with the previous 'do' and it has its own brackets.

Also inside the while you write:

while ( hours < 0 )

'hours' is an array. You cannot do these kind of calculations with an array. I believe that this is what you wanted:

while ( hours[index] < 0 )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.