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

Your code tags should end.

Also you need to be more descriptive. What your program is suppose to do? What kind of errors do you get and what output

boolean variable "stop" is a sentinel flag. If "stop" is true the "while loop" must terminate immediately. So you should check the empName to see if it is "stop" or not. Therefore, the lines of code starting for checking the name would be:

if( empName.equalsIgnoreCase("stop"))
     stop=true;  // stop becomes true and the excution jumps out of the  "while loop" immediately 
     else { // if the stop is true this code block will not be executed
          System.out.printf( "What is the hourly rate?");
          wage = input.nextFloat(); // in this way the wage is initialized
          .......

Can you manage to modify your program accordingly?

Also don't start a new thread with the same question and unformatted code. You think we have the time to keep track and answer 2 identical thread? If you have something to add do it here.

And if the compiler says that variable wage might not have been initialized then just initialize it with a value when you declare it.

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

My program is supposed to end when i enter "stop" as the employee name. thats the major issue i am having. I want to use the boolean stop=false; statement.

tong1,

I changed it to your reccomendation. I still get an error message though. I will show you at the bottom

//Filename: Payroll2.java
//Description: Payroll calculator with sentinel loop
//Author Name: Chris Russo
//Date: 08/15/2010

import java.util.Scanner;

public class Payroll2
{
	//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;





			System.out.printf( "Enter employee name or 'stop' to exit:");
			empName = input.nextLine();

			if(empName.equalsIgnoreCase("stop"))
			stop=true;
			else{
				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 main


}//end class

--------------------Configuration: <Default>--------------------
C:\Documents and Settings\chris\Desktop\Payroll2.java:40: variable wage might not have been initialized
while (wage <=0)
^
1 error

Process completed.

Initialize wage to some value when you declare it i.e.

float wage = 0f;

The compiler doesn't variables that may not have been assigned a value by the time they are used.

I am not sure if thats it. I had the program working until i started messing with the boolean stop=false; statement and changing some other things.

The error is within this block:

while (wage <=0){
   System.out.print("Please enter an amount above 0 dollars");
   wage = input.nextFloat();
}

--------------------Configuration: <Default>--------------------
C:\Documents and Settings\chris\Desktop\Payroll2.java:20: cannot find symbol
symbol : variable Of
location: class Payroll2
float wage = Of;
^
1 error

Process completed.

That is zero float, 0f , not the word 'of'.

ok, i see you were assigning a hex value correct? All i need now is to get the program to continue running until i enter "stop". It still wont exit when i input "stop".

No, it is not a hex value. The 'f' indicates a float value. So '1f' is 1 float, as opposed to a double. Hex values would be prefixed with '0x'.

Now you just need to put the while (!stop){ loop back in place.

capsitan, you made a mistake. You should remove the curly bracket in line 38 (i.e. the line 26 in the following modified code) so that the wage would be assigned a value:
wage = input.nextFloat(); // line 25 in the following modified code.
Following is the code I have modified:

import java.util.Scanner;

public class Payroll2
{
	//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;

			System.out.printf( "Enter employee name or 'stop' to exit:");
			empName = input.nextLine();

			if(empName.equalsIgnoreCase("stop"))
			stop=true;
			else{
				System.out.printf( "What is the hourly rate?");
				wage = input.nextFloat();
				// the curly bracket '}' has been removed 
			
			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 main
}//end class

thank you tong1, but i still need the program to loop continuosly until 'stop' is entered for employee name. Do i have to change the entire program in order to do this?

No, you have to make a small chage to meet the request.
Please just make while loop (i.e. while( !stop) {....}) to cover the code from 18 to 66
After the while loop, we should have only the 3 lines of code left:

System.out.println( "Bye for now.");
	}//end main
}//end class

As long as the client types in "stop", the boolean flag becomes true,resulted in violating the while loop condition ( ! top ), so that the loop terminates.
Finally, you have to say "Good Bye" to your customer.

Attached please find the code modified for your reference

You may also save the Sentinel flag (boolean stop)by using "break" to terminate the while loop (" while(true){...}).
The guard for inputed name is made of

if(empName.equalsIgnoreCase("stop")){ // guard for input name
			System.out.println("See you later.");	
			break;  //  jump out of the loop
			}

We have no need to use "if(){ ...}; else{...}" any more. Only "if(){}" is enough.
However, you have to say "Good Bye!" before jumping out of the loop (the "break"). Why?

import java.util.Scanner;

public class Payroll2
{
	//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;
		
			while( true ){ 
			System.out.printf( "Enter employee name or 'stop' to exit:");
			empName = input.next();

		if(empName.equalsIgnoreCase("stop")){  // guard for the input name
		System.out.println("See you later.");	
		break; // jump out of the loop
		} // no need else{..}

			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);
			}
		}
	}//end main
}//end class
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.