Daniel
2012-07-22 06:50:19 UTC
CheckPoint
Payroll Program Part 2 Resource: Ch. 4 & 5 of Java: How to Program
Modify the Payroll Program application so it continues to request department information until the user enters stop as the department name. In addition, program the application to check that the number of employees and average salary per employee are positive numbers. If either the number of employees or the average salary per employee is not a positive value, the application should prompt the user to enter a positive amount.
Post as an attachment in Java format.
My Code:
import java.util.Scanner; // import Java package to use the Scanner class.
import java.text.NumberFormat; // import Java package used to format currencypublic class
public class PayrollProgram { // begin DisplayPersonProgram class
public static void main(String[] args) {//create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);//variables declared
NumberFormat nf = NumberFormat.getCurrencyInstance(); // used to format currency
String departmentName; //stores department name
int numberOfEmployees;//stores number of employees
double averagePayRate;//stores average payrate
double departmentPayroll;//stores department payroll as average payrate times number of employees
boolean stop = false;// stores false for the word stop
System.out.println("Please enter the department name:");//prompts user for department name
departmentName = input.nextLine();//prompts user for departmentname
if (departmentName.equalsIgnoreCase("stop")) {
System.out.println("Exiting Program");
stop = true;
}
while (!"stop".equals(departmentName)) {
{
System.out.println("Please enter department name:");
departmentName = input.nextLine();//prompts user for departmentname
}
{
System.out.println("Please enter the number of employees:");//prompts user for number of employees information
numberOfEmployees = input.nextInt();//stores information
if (numberOfEmployees < 0.0){
System.out.println("Input must be a positive number, reenter number of employees: ");
System.out.println("Please enter the number of employees:");//prompts user for number of employees information
numberOfEmployees = input.nextInt();//stores information
}
System.out.println("Please enter the average salary of employees in the department");//prompts user for average salary information
averagePayRate = input.nextDouble();//stores information
if (averagePayRate < 0.0) {
System.out.println("Input must be a positive number, reenter average salary: ");
System.out.println("Please enter the average salary of employees in the department "); //promp
averagePayRate = input.nextDouble();//stores information
}
departmentPayroll = averagePayRate * numberOfEmployees;//calculates the total department payroll
System.out.println("Department Name:" + departmentName);//displays the Department name
System.out.println("The total department payroll is " + nf.format(departmentPayroll));//displays the department's total payroll
}
}
}//ends main module
}//ends class PayrollProgram
My Problem:
the program runs but it needs to keep asking for department name until it recieves stop as a response, I do not know where my code is erroring, as when I run it it will cycle through but when it asks for the department name again it does not wait for an answer but instead asks the number of employees at the same time, skipping the department name'