I am doing this assignment in my Java class. I have never done any programming in my past so I am a complete beginner. I have typed up a program that will ask for employee name, the hourly rate, and the number of hours worked. The program needs to run in a loop unless "stop" is typed as the employee name. There are no errors in my code and it builds/runs OKAY except for the fact that it doesn't stop running when stop is typed into employee name. I have reviewed my code and did some research online but I cannot figure out what I am doing wrong here.
package payrollprogrampart2;
import java.util.Scanner;
public class PayrollProgramPart2 { //declares class
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String cleanInputBuffer;
String employeeName;
double hourlyRate = -1d;
double hoursWorked = -1d;
double weeklyPay;
boolean end = false; //is the input name stop?
while( end == false ) //as long as end is false, continue
{
System.out.print("Enter employee name or enter stop to quit: ");
employeeName = input.nextLine();
if( employeeName.toLowerCase().equals("stop"))
end = true;
while(hourlyRate < 0)
{
System.out.print("Enter a positive hourly rate: $");
hourlyRate = input.nextDouble();
}
while(hoursWorked < 0)
{
System.out.print( "Enter a positive number of hours worked: ");
hoursWorked = input.nextDouble();
}
weeklyPay = hourlyRate * hoursWorked;
System.out.printf( "The employee %s was paid $ %.2f this week", employeeName, weeklyPay);
cleanInputBuffer = input.nextLine();
}
}
}
Here is the output:
Enter employee name or enter stop to quit: Kayla
Enter a positive hourly rate: $11
Enter a positive number of hours worked: 80
The employee Kayla was paid $ 880.00 this weekEnter employee name or enter stop to quit: stop
The employee stop was paid $ 880.00 this weekstop
BUILD SUCCESSFUL (total time: 15 seconds)
How can I get the program to skip to a next line when asking for second employee name? Also, when I type stop into employee name, it doesn't stop the program and it gives me the amount from the previous employee but names the employee stop. Why is this? I am so confused!! :[