sentinel controlled while loops in java
807601Mar 20 2008 — edited Mar 20 2008Hi,
I am a newbie to java and a program that we have been given is to have a user continually enter information until a sentinel of "stop" is given. My problem is that in my while loop the user never gets to enter their name again. I have included the code below. I know I am going in the right direction, but can't seem to get the program to work right. Can anyone assist?
Thanks.
import java.util.Scanner; // program uses class Scanner
public class Payroll
{
// main method begins here for input of information and calculation
public static void main( String args[] )
{
String employeeName1 = "";
System.out.println( "Welcome to the wage calculator!\nThis program will ask for an employee, their hours worked and wage.\nIt will then output the name and total wages for the week." );
System.out.println();
double hourlyRate1; // Variable for the employee's hourly rate of pay
int hoursWorked1;// Variable for the employee's hours worked in one week
double sum; // The sum of the hourly rate multiplied by the hours worked
// Initialize scanner for input from user
Scanner input = new Scanner( System.in );
System.out.print( "Enter the name of the employee, enter stop to quit: " );
employeeName1 = input.nextLine();// Input for employee name
while(!employeeName1.equals("stop"))
{
System.out.print( "Enter the hourly rate of pay for the employee: " );
hourlyRate1 = input.nextDouble();// Input variable for the hourly wage rate
while ( hourlyRate1 < 0 )
{
System.out.println ( "That amount is a negative number, please re-enter an amount that is above zero: " );
hourlyRate1 = input.nextDouble();
}
System.out.print( "Enter the number of hours the employee has worked for the week: " );
hoursWorked1 = input.nextInt();// Input variable for the hours worked
while ( hoursWorked1 < 0 )
{
System.out.println( "That amount is a negative number, please re-enter an amount that is above zero: ");
hoursWorked1 = input.nextInt();
}
sum = hourlyRate1 * hoursWorked1;// Calculation of total wages for the week
System.out.println();
System.out.println();
System.out.printf( "%s's ", employeeName1 );
System.out.printf( "total wages for the week are $%.2f\n\n", sum );
System.out.println();
System.out.print( "Enter the name of the employee, enter stop to quit: " );
employeeName1 = input.nextLine();// Input for employee name
System.out.println();
}
System.out.println( "Thank you for using my program!" );
} // End main methoud
} // End of class Payroll1