I am doing a payroll program that requires a sentinel value. I cant seem to get it right no matter how much I try. here is my code so far:
//Filename: Payroll3.java
//Description: Payroll calculator with sentinel loop
//Author Name: Chris Russo
//Date: 08/15/2010
import java.util.Scanner;
public class Payroll3New
{
//main method to execute program
public static void main (String args[] )
{
//main execution
Scanner input = new Scanner( System.in );
//variables
String empName;
float wage;
float hours;
float pay;
double overTimePay;
boolean stop= false;//changed to sentinel flag
while(!stop)
{
System.out.printf( "Enter employee name or 'stop' to exit:");
empName = input.nextLine();
if( empName.equalsIgnoreCase("stop")){
System.out.printf( "What is the hourly rate?");
wage = input.nextFloat();
}
while (wage <=0)
{
System.out.print("Please enter an amount above 0 dollars");
wage = input.nextFloat();
}
System.out.print( "How many hours?");
hours = input.nextFloat();
while (hours <=0)
{
System.out.print("Please enter an amount above 0 hours:");
hours = input.nextFloat();
}
if (hours>40)// Overtime
{
pay = hours * wage;//multiplication
overTimePay = (hours-40) * (wage*1.5);
//using printf and println
System.out.println("Employee:" + empName);
System.out.printf("Hourly Rate: $%.2f\n", wage);
System.out.println("Regular Hours worked:" + "40");
System.out.println("Overtime Hours worked:" + (hours-40));
System.out.printf("Total Pay: $%.2f\n", pay);
System.out.printf("Overtime Pay: $%.2f\n", overTimePay);
System.out.printf("Gross Pay: $%.2f\n", pay+overTimePay);
}
else // Under 40 Hours
{
pay = hours * wage;//multiplication
//using printf and println
System.out.println("Employee:" + empName);
System.out.printf("Hourly Rate: $%.2f\n", wage);
System.out.println("Regular Hours worked:" + hours);
System.out.printf("Gross Pay: $%.2f\n", pay);
}
System.out.printf( "Enter employee name or 'stop' to exit:");
empName = input.next();
}//end loop
}//end main
}//end class
Also, I get the following error message:
--------------------Configuration: <Default>--------------------
H:\Payroll3New.java:43: variable wage might not have been initialized
while (wage <=0)
^
1 error
Process completed.