Syntax error on system.out.println?

1.3k views Asked by At

This is for constants type double.

I am getting this syntax error for my system.out.println code:

Syntax error on token ";", @ expected

Thanks!

public final class Netpay {

public static void main(String[] args) {
    // TODO Auto-generated method stub

public static final double FEDERAL_TAX_PERCENT = 10.00;
public static final double STATE_TAX_PERCENT = 4.5;
public static final double SS_PERCENT = 6.2;
public static final double MEDICARE_PERCENT = 1.45;
public static final double PAY_PER_HOUR = 7.25;



    int hoursPerWeek = 40;

    double grossPay = hoursPerWeek * PAY_PER_HOUR;
    double federalTax = grossPay * FEDERAL_TAX_PERCENT / 100;
    double stateTax = grossPay * STATE_TAX_PERCENT / 100;
    double medicare = grossPay *  MEDICARE_PERCENT / 100;
    double socialSecurity = grossPay * SS_PERCENT / 100;
    double netPay = grossPay -  (federalTax + stateTax + medicare + socialSecurity);

    system.out.println("Hours Per Week = 40");
    system.out.println("Gross Pay= grossPay");
    system.out.println("Net Pay = netPay");

    system.out.println("Deductions:");
    system.out.println("Federal= federalTax");
    system.out.println("State = stateTax");
    system.out.println("Social Security = socialSecurity");
    system.out.println("Medicare = medicare");


}
2

There are 2 answers

0
neuronaut On

Move your constants outside of the main method:

public final class Netpay {

    public static final double FEDERAL_TAX_PERCENT = 10.00;
    public static final double STATE_TAX_PERCENT = 4.5;
    public static final double SS_PERCENT = 6.2;
    public static final double MEDICARE_PERCENT = 1.45;
    public static final double PAY_PER_HOUR = 7.25;

    public static void main(String[] args) {
        // the rest of your code goes here
    }
}

And as already mentioned, make system have an uppercase S: System.out.println.

2
hagrawal7777 On

Understand the concept - there is no point of using public and static for a variable inside a method. Since in method all variables are local so there is nothing public, private, protected etc. Now, static in Java means something which will remain in heap area for the JVM's life. Now since inside method, variables and objects are local, and will be garbage collected (until you are passing their reference etc.) once method execution completes, so no point of static as well. You cannot use static inside method, whether static or instance method.

1.Always keep the constants which you may need from different parts of application in one constants file. If you don't need it a lot then better have it as local variables inside method, or instance variables, as required.
A static instance variable can be accessed from anywhere in the application using {classname}.{variable_name}
So, your constants Java file could be:

public class ApplicationConstants {
    public static final double FEDERAL_TAX_PERCENT = 10.00;
    public static final double STATE_TAX_PERCENT = 4.5;
    public static final double SS_PERCENT = 6.2;
    public static final double MEDICARE_PERCENT = 1.45;
    public static final double PAY_PER_HOUR = 7.25;
}

2.System.out.println() is to print something to the console, and argument it accepts it is a String. So, if you will put anything inside quotation then it will be a string (that's the reason you were getting result as Gross Pay= grossPay), it you want to print a dynamic value then append it using "+" operator.

public static void main(String[] args) {
            int hoursPerWeek = 40;

            double grossPay = hoursPerWeek * ApplicationConstants.PAY_PER_HOUR;
            double federalTax = (grossPay * ApplicationConstants.FEDERAL_TAX_PERCENT) / 100;    //Corrected your formula - (A+B)/C
            double stateTax = (grossPay * ApplicationConstants.STATE_TAX_PERCENT) / 100;        //Corrected your formula - (A+B)/C
            double medicare = (grossPay * ApplicationConstants.MEDICARE_PERCENT) / 100;         //Corrected your formula - (A+B)/C
            double socialSecurity = (grossPay * ApplicationConstants.SS_PERCENT) / 100;         //Corrected your formula - (A+B)/C
            double netPay = grossPay
                    - (federalTax + stateTax + medicare + socialSecurity);

            System.out.println("Hours Per Week = " + hoursPerWeek);
            System.out.println("Gross Pay = " + grossPay);
            System.out.println("Net Pay = " + netPay);

            System.out.println("Deductions:");
            System.out.println("Federal = " + federalTax);
            System.out.println("State = " + stateTax);
            System.out.println("Social Security = " + socialSecurity);
            System.out.println("Medicare = " + medicare);
        }