So I have been looking around and have not been able to find an answer to this. The program is for the most part complete and runs with one exception. When I enter "stop" it still runs through the program before exiting (i.e. after I enter stop it still asks for hourly rate and hours worked before exiting the program). I am just wondering what I could do to fix this so that when I type stop the program immediately exits. Thanks in advance for your response!

Here is my code:

// Week 3 - Day 5 - Payroll Program Part 2 
// John Sanders 
// IT 215

import java.util.Scanner; // class scanner

public class Payroll2 // set public class

{ 
	
	public static void main( String args[] )
	
	{
		Scanner input = new Scanner( System.in );
		
		String cleanInputBuffer; // input
		String empName; // input employee name
		double hourlyRate; // input hourly rate
		double hoursWorked; //input hours worked
		double weeklyPay; // weekly pay amount
		boolean end = false; // is the input name stop?
		while( end == false ) // as long as end is false, continue
		
		{
			
			System.out.print( "Enter Employee's Name:" ); // prompt to enter employee name
			empName = input.nextLine(); // input employee name
			if( empName.toLowerCase().equals( "stop" )) // if employee name = stop
				end = true;  // when stop is detected, change the boolean, which ends the while loop
		
		while( hourlyRate < 0 ) // while the hourly rate is < 0
		
		{
				
			System.out.print( "Enter a positive hourly rate:" ); // print enter a positive hourly rate
				hourlyRate = input.nextDouble();
				
		}
		
		while( hoursWorked < 0 ) // while the hours worked are < 0
		
		{
			System.out.print( "Enter a positive number of hours worked:" ); // print enter a positive number of hours worked
			hoursWorked = input.nextDouble();
			
		}
		
		weeklyPay = hourlyRate * hoursWorked; // multiply hourly rate by hours worked for weekly pay
		
		System.out.printf( "The employee %s was paid $ %.2f this week", empName, weeklyPay ); // print final line
		System.out.println();
		
		cleanInputBuffer = input.nextLine();
		
		} // end outer while
		
	} // end main method
	
} // end class Payroll2

Recommended Answers

All 6 Replies

Initialise both

double hourlyRate; // input hourly rate
double hoursWorked; //input hours worked

with -1d value.
Inside while-loop refresh them with same -1d value

So I have been trying out what you suggested but am not quite sure what it means. I am very new to this and struggling pretty bad. I have been researching the internet, re-reading my school reading, and even glancing through a Java for Dummies book, and cant seem to find anything that deals with this. If Java was required to be an IT I think I would opt for working at McDonalds the rest of my life (which by the way, I do not work at one!) Below I have posted what I think you mean, but it did not work however.

String cleanInputBuffer; // input
		String empName; // input employee name
		double hourlyRate = -1d; // input hourly rate
		double hoursWorked = -1d; //input hours worked
		double weeklyPay; // weekly pay amount
		boolean end = false; // is the input name stop?
		while( end == false ) // as long as end is false, continue

I am a little unsure as to how to "refresh" them inside the while loop. I have tried many different combos of adding to achieve this but non have worked. (i.e. hoursWorked = hoursWorked(-1d))

With

double hourlyRate = 0;
   double hoursWorked = 0;

result is no good:

run:
Enter Employee's Name:AAA
The employee AAA was paid $ 0,00 this week

Below it's OK

double hourlyRate = -1d; // input hourly rate
   double hoursWorked = -1d; //input hours worked

if you first time goes inside while-loop
hourlyRate = -1d and hourlyRate = -1d
you made a code inside

while (hourlyRate < 0) {
                System.out.print("Enter a positive hourly rate:"); // print enter a positive hourly rate
                hourlyRate = input.nextDouble();
            }
            while (hoursWorked < 0) {
                System.out.print("Enter a positive number of hours worked:"); // print enter a positive number of hours worked
                hoursWorked = input.nextDouble();
            }

now result is

run:
Enter Employee's Name:AAA
Enter a positive hourly rate:8
Enter a positive number of hours worked:10
The employee AAA was paid $ 80,00 this week
Enter Employee's Name:BBB    // new Employee
The employee BBB was paid $ 80,00 this week  // but old data

you can accept new values from scanner, but the last line is wrong because
still hourlyRate = 8 and hoursWorked =10 fo new Employee.
With this old values in second traverse you never reach new values from scanner.
As I said refresh this values, simply write

hourlyRate = -1;
   hoursWorked = -1;

you can do this after print final line
Result :

run:
Enter Employee's Name:AAA
Enter a positive hourly rate:8
Enter a positive number of hours worked:10
The employee AAA was paid $ 80,00 this week
Enter Employee's Name:BBB
Enter a positive hourly rate:4
Enter a positive number of hours worked:5
The employee BBB was paid $ 20,00 this week
Enter Employee's Name:stop
Enter a positive hourly rate:1111111111  //  WRONG
Enter a positive number of hours worked:11111111111
The employee stop was paid $ 12345679010987655000,00 this week
BUILD SUCCESSFUL (total time: 52 seconds)

stop is not employee name

I would suggest putting the rest of your code in the while(end==false) in the else part of this if condition :-

if(empName.toLower().equals("stop")) {
  end = true;

That would make it skip the rest of the loop

Or you could also just put a "break" statement inside the same "if" block.

EDIT:
Reply is referring to your first post only,

write break;
after
if(empName.toLower().equals("stop")) { end = true;if(empName.toLower().equals("stop")) {
end = true;

so the code will be
if(empName.toLower().equals("stop")) { end = true;if(empName.toLower().equals("stop")) {
end = true;
break;

i hope it's helpful

@hosam
If you are going to give the code at least use code tags.

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.