46: Unreachable Statement
Code :
import java.util.Scanner;
public class Quiz9
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
Utility util1 = new Utility();
Payroll emp1 = new Payroll();
String strFirstName;
String strLastName;
String strSocialSecurityNumber;
String strInfo;
double dTotalPay;
double dTotalAverage;
double dRateOfPay;
double dHoursWorked;
int iNumberOfDependents;
while(true)
{
util1.clearScreen();
System.out.print("What is the users first name?");
strFirstName = kb.nextLine();
emp1.setFirst(strFirstName);
System.out.print("What is the user's last name?");
strLastName = kb.nextLine();
emp1.setLast(strLastName);
System.out.print("What is the user's Social Security Number?");
strSocialSecurityNumber = kb.nextLine();
emp1.setSSN(strSocialSecurityNumber);
System.out.print("What is the user's rate of pay?");
dRateOfPay = kb.nextDouble();
emp1.setRate(dRateOfPay);
System.out.print("How many hours did the user work?");
dHoursWorked = kb.nextDouble();
emp1.setHours(dHoursWorked);
System.out.print("How many dependants does the user have?");
iNumberOfDependents = kb.nextInt();
emp1.setDependents(iNumberOfDependents);
strInfo = emp1.toString();
System.out.print(strInfo);
util1.pressEnterToContinue();
util1.clearScreen();
}//end of while true
if(dHoursWorked == 0)
{
System.exit(0);
}
}
}
Why is my if statement unreachable?
Also, how would I take the total of all the wages entered and output them at the end of the program?
Also, why can I not input a first name for an employee after the first run?
Thanks for the help!
Re: 46: Unreachable Statement
When do you expect that while loop to exit?
Re: 46: Unreachable Statement
I want the if loop to end when I enter 0 for the amount of hours worked.
Re: 46: Unreachable Statement
That's a while loop- there is no such thing as an "if loop". But that while loop is while(true), which will never exit. Your System.exit() call is after that while loop, and since that while loop will never exit, that call will never be reachable.
Re: 46: Unreachable Statement
Alright, I have another questions, and thanks. I am calling the emp1.setRate from a different java class, and I want to know how to total all of the numbers that I input. Can you be of any assistance with that?
Re: 46: Unreachable Statement
Maybe, but why are you setting the rate of a single instance of Employee multiple times? Don't you want multiple Employees, each with a different rate?