Hello,

I need help badly. I was able to compile the program but when i ran it, it didn't do what it is supposed to do.

The program should continue to promt for next employee's info until user enters "stop".

I am not sure what I am missing.. Any help would be very much appreciated.

------

import java.util.Scanner;

public class PayrollProgramPart2
{
	public static void main( String args[] )
		{
		System.out.println( "Welcome to the Payroll Program!" );

		boolean stop = false; // Loop until user types "stop" as the employee name.

			{

	Scanner input = new Scanner(System.in );

	System.out.print( "Enter Employee's Name or stop to exit program: " );
		String empName = input.nextLine(); // employee name

					if ( empName.equals("stop"))
				{
		System.out.println( "Program Exited" );
			stop = true;

				}
					else
				{
					double hourlyRate; // hourly rate
					double hoursWorked; // hours worked
					double weeklyPay; // Weekly Pay for employee

			System.out.print( "Enter hourly rate: " ); // prompt for hourly rate
			hourlyRate = input.nextDouble();

					while (hourlyRate <= 0) // prompt until positive value is entered
				{
			System.out.print( "Hourly rate must be a positive number. " +
			"Please enter the hourly rate again: " );
			hourlyRate = input.nextDouble(); // read hourly rate again
				}

			System.out.print( "Enter hours worked: " ); // prompt for hours worked
			hoursWorked = input.nextDouble();


					while (hoursWorked <= 0) // prompt until a positive value is entered
				{
			System.out.print( "Hours worked must be a positive number. " +
			"Please re-enter hours worked: " ); // prompt for positive value for hours worked
			hoursWorked = input.nextDouble(); // read hours worked again
				}

	weeklyPay = (double) hourlyRate * hoursWorked; // multiply sum

	System.out.printf( "%s%.2f\n",
   "Employee: " + empName + " Weekly Pay:", weeklyPay ); // display employee's weekly gross income in U.S. dollar

				}
			}

	} // end method main

} // end class PayrollPart2

Recommended Answers

All 2 Replies

Here is what you have:

boolean stop = false;
{
Scanner input = new Scanner(System.in );
	System.out.print( "Enter Employee's Name or stop to exit program: " );
		String empName = input.nextLine(); // employee name

if ( empName.equals("stop")) {
		System.out.println( "Program Exited" );
		stop = true;
} else {
      while (hourlyRate <= 0) 
				{...
				}
	while (hoursWorked <= 0) 
				{....
				}
	}
}

In the same way you repeat the code when the user enters a negative value:

while (hoursWorked <= 0)
{....
}

You will have to repeat the entire code until the user enters stop. You already have a boolean that you set it from false to true. So use another while in which you will put the whole code I have posted it and use that boolean variable inside the while statement

Thank you...

I found what I was missing.

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.