Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Java
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!! :[
Posted

1 solution

1. to exit a loop, use break, e.g.
C#
if( employeeName.toLowerCase().equals("stop"))
      // end = true;
      break; // exit the while loop

2.
while(hourlyRate < 0)

and
while(hoursWorked < 0)

are always true when they are given positive value the previous time.
use do...while instead, e.g.
C#
do {
      System.out.print("Enter a positive hourly rate: $");
      hourlyRate = input.nextDouble();
} while (hourlyRate < 0)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900