Question:
I cannot get my program to run correctly, can somebody tell me where I am going wrong?
Daniel
2012-07-22 06:50:19 UTC
Assignment:
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'
Four answers:
AnalProgrammer
2012-07-22 07:29:35 UTC
You do seem to have too many curly braces though this is not a problem. Just adds clutter.

You also seem to have all the right code, just not necessarily in the right order.

I have also added a dummy read that fixes your problem.



I cannot show the whole of your program so I hope this works.



System.out.println("Please enter the department name:");//prompts user for department name



departmentName = input.nextLine();//prompts user for departmentname

while (!"stop".equals( 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



String dummy = input.nextLine(); // dummy read to get rid of stray newline.



System.out.println("Please enter department name:");

departmentName = input.nextLine();//prompts user for departmentname



} //ends while loop

if (departmentName.equalsIgnoreCase( "stop")) {

System.out.println("Exiting Program");

stop = true;

}



}//ends main module



Have fun.
galt_57
2012-07-22 15:40:15 UTC
I think using only scanner.nextLine() solves a lot of these sort of issues. You can then parse the string to the desired type inside a try-catch. The parse will throw an exception if the conversion is impossible. If you use a type-specific scan such as scanner.nextInt() then you will still need a try-catch because the scan will throw mismatch exceptions and you will need to use scanner.nextLine() in the catch to clean out the rejected input. Also scanner.nextInt() waits for only the first time, so if the user enters several integers on the same line you can get some unexpected looping as each integer is grabbed and processed.



As it is your program only accepts one department? If not you need to make departmentName an array or arrayList and add an outside do-while loop. You don't test that departmentName has a minimum length so what if someone enters a blank line for the department name?
Steve M
2012-07-22 14:45:19 UTC
Someone will probably just give you the code, but it's important to know why the code is behaving the way it is. The reason it skips your input is because input.nextInt() and input.nextDouble() only read a portion of the input buffer into your variable, up until whitespace, the rest of the input line stays for the next scanner call. The next time you call input.nextLine(), it's just reading the rest of that line (even if it's just the new line or enter key). So placing an extra call to input.nextLine() after the input.nextDouble() and just ignoring the result will clear the input buffer so you're ready to start again.
anonymous
2012-07-22 14:15:22 UTC
I don't know Java all that well but isn't there a problem with your { } on those lines ?



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


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...