Re: final error and i cant figure this out.OMG
tyrelma...@ wrote:
Im almost finished and this code is giving me an error.i [sic] have been trying to fix it for 6 hours
now. can someone please help me. the comiler error keeps saying, " identifer expected, illigel start of
type. this is my code:
In future, COPY and PASTE error messages. For one thing, that will avoid misspellings, but more
importantly it will provide more information for those inclined to help you.
Such as, for example, what line of code triggered the error, and what position in that line.
Why would you omit that vital information anyway?
package sheridan;
import java.util.Scanner;
public class PayrollApplication
{
public static void main (String[] args)
{
PayrollApplication app = new PayrollApplication();
app.run();
}
private void run()
If you name a no-arg, void-returning method 'run()', you should consider making
it public. It's the traditional name for a public go-ahead method such as 'Runnable'
sports.
{
Employee employeeInfo = new Employee();
System.out.println ("Enter employee's name:");
Scanner input = new Scanner(System.in);
String employeeName = input.nextLine();
if (employeeName.length()==0)
{
System.out.println("Not Entered");
}
System.out.println("Enter number of hours worked in a week:");
double weeklyHoursFromUser = input.nextDouble();
employeeInfo.setWeeklyHours(weeklyHoursFromUser);
if (weeklyHoursFromUser <= 0)
{
System.out.println("You have entered an incorrect number of hours");
return;
}
System.out.println("Enter your hourly pay rate");
double hourlyPayRateFromUser = input.nextDouble();
employeeInfo.setHourlyPayRate(hourlyPayRateFromUser);
if (hourlyPayRateFromUser <= 0)
{
System.out.println(" you have entered an incorrect pay rate ");
return;
}
System.out.println("Enter Federal tax withholding rate");
double federalTaxRateFromUser = input.nextDouble();
employeeInfo.setFederalTaxRate(federalTaxRateFromUser);
if (federalTaxRateFromUser <=0 || federalTaxRateFromUser>=1)
{
System.out.println(" You have entered an incorrect amount for the Federal tax. Please run the program again and enter a real number between 0 and 1 exclusively.");
"... between 0 and 1, exclusive."
"Exclusively" doesn't mean what you mean to mean.
return;
}
System.out.println("Enter State tax withholding rate");
double stateTaxRateFromUser = input.nextDouble();
employeeInfo.setStateTaxRate(stateTaxRateFromUser);
if (stateTaxRateFromUser <=0 || stateTaxRateFromUser>=1)
{
System.out.println("You have entered an incorrect amount for the State tax. Please run the program again and enter a real number between 0 and 1 exclusively.");
return;
}
calculateUserPay();
}
private double calculateUserPay()
{
Employee employeeInfo = new Employee();
double weeklyHours = employeeInfo.getWeeklyHours();
double payRate = employeeInfo.getHourlyPayRate();
double federalTax = employeeInfo.getFederalTaxRate();
double stateTax = employeeInfo.getStateTaxRate();
if(weeklyHours<40)
{
double netPay = payRate * weeklyHours - federalTax + stateTax;
return netPay;
}
else
{
double overTime = 40 * payRate + (weeklyHours - 40)*(payRate * 1.5) - federalTax + stateTax;
printStatement();
}
}
private void printStatement()
{
System.out.println ("Employee Name:"+ employeeName);
And just where is 'employeeName' declared?
System.out.println ("Hours worked:" + weeklyHoursFromUser );
Ditto 'weeklyHoursFromUse'.
System.out.println ("Pay rate:"+ hourlyPayRateFromUser);
etc.
System.out.println ("Gross pay:"+ hourlyPayRateFromUser * weeklyHoursFromUser );
System.out.println ("Deductions" );
System.out.println ("Federal Withholding" + federalTaxRateFromUser*(hourlyPayRateFromUser * weeklyHoursFromUser));
System.out.println ("State Withholding" + stateTaxRateFromUser * (hourlyPayRateFromUser * weeklyHoursFromUser));
System.out.println ("Total deduction:" + weeklyHoursFromUser * hourlyPayRateFromUser-( federalTaxRateFromUser*(hourlyPayRateFromUser * weeklyHoursFromUser))-(stateTaxRateFromUser * (hourlyPayRateFromUser * weeklyHoursFromUser)));
System.out.println ("Net Pay:" );
}
Variables have to be declared before you can use them.
and my Employee class is:
package sheridan;
import java.util.Scanner;
public class Employee
{
Employee employeeInfo = new Employee();
BRAAAAAHHHHH!
How can you instantiate an 'Employee' inside an 'Employee''s instantiation, as
John B. Matthews pointed out?
private double _weeklyHours;
Don't use underscores in variable names unless they're constant variables (or at
least final references to immutable instances).
private double _hourlyPayRate;
private double _federalTaxRate;
private double _stateTaxRate;
public double getWeeklyHours()
{
return _weeklyHours;
}
public void setWeeklyHours (double newWeeklyHoursParam)
{
_weeklyHours = newWeeklyHoursParam;
}
public double getHourlyPayRate()
{
return _hourlyPayRate;
}
public void setHourlyPayRate (double newHourlyPayRateParam)
{
_hourlyPayRate = newHourlyPayRateParam;
}
public double getFederalTaxRate()
{
return _federalTaxRate;
}
public void setFederalTaxRate (double newFederalTaxRateParam)
{
_federalTaxRate = newFederalTaxRateParam;
}
public double getStateTaxRate()
{
return _stateTaxRate;
}
public void setStateTaxRate (double newStateTaxRateParam)
{
_stateTaxRate = newStateTaxRateParam;
}
}
--
Lew