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));
}
}
}