I'm stumped, and I know it is simple.
Here's what I need this to do:
Quote:
|
Modify the Payroll Program application so it continues to request employee information until the user enters stop as the employee name. In addition, program the application to check that the hourly rate and number of hours worked are positive numbers. If either the hourly rate or the number of hours worked is not a positive value, the application should prompt the user to enter a positive amount.
|
never mind the original program... I had it right. Here is what I have right now:
Quote:
// Employee Payroll Program 2
// Acquire employee name, hourly rate, and hours worked,
// then display employee name and weekly wage
import java.util.Scanner; // program uses Scanner
public class Payroll2
{
// main method begins execution of Java application
public static void main( String args[] )
{
// Welcome message for Employee Payroll Program
System.out.println( "Welcome to the Employee Payroll Program" );
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// prompt for and input employee name
System.out.println( "Please enter the employee's name or " );
System.out.println( "enter \"stops\" to exit the program." );
String employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line
while ( employeeName != "stops" )
{
// prompt for and input hourly rate
double hourlyRate; // hourly rate amount read from user
System.out.println( "Please enter the employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input
while ( hourlyRate < 0 )
{
System.out.println( "Hourly rate must be a positive number." ); // error message
System.out.println( "Please enter employee's hourly rate: " ); // prompt
hourlyRate = input.nextDouble(); // obtain user input
} // end while
System.out.println(); // outputs a blank line
// prompt for and input number of hours worked
double hoursWorked; // number of hours worked read from user
System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input
System.out.println(); // outputs a blank line
while ( hoursWorked < 0 )
{
System.out.println( "The number of hours worked must be a positive number." ); // error message
System.out.println( "Please enter the number of hours worked: " ); // prompt
hoursWorked = input.nextDouble(); // obtain user input
} // end while
// compute weekly pay
double weeklyPay; // set weekly pay as primitive variable
weeklyPay = hourlyRate * hoursWorked;
// display employee name and weekly pay
System.out.printf( "The employee's name is %s\n", employeeName );
System.out.println(); // outputs a blank line
System.out.printf( "The weekly pay is $ %.2f\n", weeklyPay );
System.out.println(); // outputs a blank line
// prompt for and input employee name
System.out.println( "Please enter the employee's name or " );
System.out.println( "enter \"stops\" to exit the program." );
employeeName = input.nextLine(); // read a line of text
System.out.println(); // outputs a blank line
} // end while
} // end main
} //end class Payroll2
|
I originally had "String employeeName = input.nextLine(); // read a line of text" as the next to last line before the end of the main while loop, but I kept getting a compilation error because I had already declared employeeName earlier.
My problem now is that the program skips over letting the user enter anew name once it goes through the first time. It prints the prompt, but skips the input.
If anyone can help, please jump in. I am supposed to submit this in two hours.
EDIT: It delete all my white space... sorry about that.