Results 1 to 16 of 16
  1. #1
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Angry NetPay java programming error-Need help

    I'm very new to Java and cannot figure out my error. I'm writing a program called NetPay.java using tax rate info from a list, which is in my coding. I use a scanner class, ask the user to enter his hourly wage and the number of hours worked for the week. Output this information in the command window along with the gross pay, each of the taxes and the net pay for the week. This is what I have so far, followed with the errors.

    import java.util.Scanner;

    public class NetPay
    {
    public static void main (String [] args)
    {
    //declare and initialize variables
    String output;
    double beforeTaxPay,
    overtimePay,
    weeklyPay,
    payRate,
    fullPay,
    totalTaxRate;
    int hoursWorked;
    final double federalTaxRate = .1,
    stateTaxRate = .05,
    localTaxRate = 0.25;

    Scanner input = new Scanner (System.in); //For scanner use

    //Input

    System.out.print( "Enter hours worked this week: ");

    hoursWorked = input.nextInt();
    System.out.print( "Enter your weekley pay rate: ");
    payRate = input.nextDouble();

    //Processing

    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    fullPay = hoursWorked * payRate;
    weeklyPay = ( fullPay + overtimePay ) * totalTaxRate;
    beforeTaxPay = fullPay + overtimePay;

    if ( fullPay > 1000)
    {
    federalTaxRate = .20;
    stateTaxRate = .10;
    localTaxRate = .05;
    }
    else
    {
    federalTaxRate = .10;
    stateTaxRate = .05;
    localTaxRate = .025;
    }

    //Output

    output = " Total hours entered =" + hoursWorked + "\n" +
    " Pay Rate entered =" + payRate + "\n\n" +
    " Before taxes, your weekly pay is : " + beforeTaxPay + "\n" +
    " After taxes, your weekly pay is : " + weeklyPay + "\n\n";
    System.out.println( output );
    }
    }


    ERRORS:
    NetPay.java:45: error: cannot assign a value to final variable federalTaxRate
    federalTaxRate = .20;
    ^
    NetPay.java:46: error: cannot assign a value to final variable stateTaxRate
    stateTaxRate = .10;
    ^
    NetPay.java:47: error: cannot assign a value to final variable localTaxRate
    localTaxRate = .05;
    ^
    NetPay.java:51: error: cannot assign a value to final variable federalTaxRate
    federalTaxRate = .10;
    ^
    NetPay.java:52: error: cannot assign a value to final variable stateTaxRate
    stateTaxRate = .05;
    ^
    NetPay.java:53: error: cannot assign a value to final variable localTaxRate
    localTaxRate = .025;
    ^
    6 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    You should format your code using code tags.

    Once you initialize final variables, you can't assign a value to them again. So, don't initialize them at the beginning of the program if you are going to assign a value later in the program or don't make them final variables.

    Also, you need to move the following lines of code to a point after the if condition:
    Java Code:
    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    weeklyPay = ( fullPay + overtimePay ) * totalTaxRate;
    Stephen

  3. #3
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Hello,
    Thanks for the post. I've read your code carefully and a couple of things are unclear to me. Firstly, the calculation of weeklyPay includes the overtimePay variable. However, the overtimePay amount isn't caluclated anywhere in the program. Secondly, the calculation of weekly pay should be

    weeklyPay = fullPay + overtimePay - ( fullPay + overtimePay ) * totalTaxRate;

  4. #4
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Thanks so much for the help and that helped me fixed my errors.


    Quote Originally Posted by shall View Post
    You should format your code using code tags.

    Once you initialize final variables, you can't assign a value to them again. So, don't initialize them at the beginning of the program if you are going to assign a value later in the program or don't make them final variables.

    Also, you need to move the following lines of code to a point after the if condition:
    Java Code:
    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    weeklyPay = ( fullPay + overtimePay ) * totalTaxRate;
    Stephen

  5. #5
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    I think I'm missing a line of code. Its not calculating the taxes.


    Quote Originally Posted by shall View Post
    You should format your code using code tags.

    Once you initialize final variables, you can't assign a value to them again. So, don't initialize them at the beginning of the program if you are going to assign a value later in the program or don't make them final variables.

    Also, you need to move the following lines of code to a point after the if condition:
    Java Code:
    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    weeklyPay = ( fullPay + overtimePay ) * totalTaxRate;
    Stephen

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    It should be calculating the tax. Post your code.

    Also, what's the use of using overtimePay in your weekly pay calculations if your not computing it?

    Remember to use code tags. To do so, just click the "Go Advanced" button right below Quick Reply and then choose the code tag icon (put your mouse over the icon to get help).

  7. #7
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    I'm not computing overtime after all so that has been removed. I'm not getting any error messages either.

    import java.util.Scanner;

    public class NetPay
    {
    public static void main (String [] args)
    {
    //declare and initialize variables
    String output;
    double beforeTaxPay,
    overtimePay,
    weeklyPay,
    payRate,
    fullPay,
    totalTaxRate;
    int hoursWorked;
    double federalTaxRate = .1,
    stateTaxRate = .05,
    localTaxRate = 0.25;

    Scanner input = new Scanner (System.in); //For scanner use

    //Input

    System.out.print( "Enter hours worked this week: ");

    hoursWorked = input.nextInt();
    System.out.print( "Enter your weekley pay rate: ");
    payRate = input.nextDouble();

    //Processing

    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    fullPay = hoursWorked * payRate;
    weeklyPay = fullPay * totalTaxRate;
    beforeTaxPay = fullPay;

    if ( fullPay > 1000)
    {
    federalTaxRate = .20;
    stateTaxRate = .10;
    localTaxRate = .05;
    }
    else
    {
    federalTaxRate = .10;
    stateTaxRate = .05;
    localTaxRate = .025;
    }

    //Output

    output = " Total hours entered =" + hoursWorked + "\n" +
    " Pay Rate entered =" + payRate + "\n\n" +
    " Before taxes, your weekly pay is : " + beforeTaxPay + "\n" +
    " After taxes, your weekly pay is : " + weeklyPay + "\n\n";
    System.out.println( output );
    }
    }


    This is what it shows when I compile and run:
    Enter hours worked this week: 40
    Enter your weekley pay rate: 500
    Total hours entered =40
    Pay Rate entered =500.0

    Before taxes, your weekly pay is : 20000.0
    After taxes, your weekly pay is : 8000.0



    ----jGRASP: operation complete.

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    You want to determine the tax rate before you compute the tax, so move the following code to a point after the if statement (NOT inside the if).

    Java Code:
            totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
            fullPay = hoursWorked * payRate;
            weeklyPay = fullPay * totalTaxRate;
            beforeTaxPay = fullPay;

  9. #9
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Like this?

    //Processing

    if ( fullPay > 1000)
    {
    federalTaxRate = .20;
    stateTaxRate = .10;
    localTaxRate = .05;
    }
    else
    {
    federalTaxRate = .10;
    stateTaxRate = .05;
    localTaxRate = .025;

    }
    totalTaxRate = federalTaxRate + stateTaxRate + localTaxRate;
    fullPay = hoursWorked * payRate;
    weeklyPay = fullPay * totalTaxRate;
    beforeTaxPay = fullPay;

    //Output

    output = " Total hours entered =" + hoursWorked + "\n" +
    " Pay Rate entered =" + payRate + "\n\n" +
    " Before taxes, your weekly pay is : " + beforeTaxPay + "\n" +
    " After taxes, your weekly pay is : " + weeklyPay + "\n\n";
    System.out.println( output );
    }
    }

    I now get this error message:
    NetPay.java:38: error: variable fullPay might not have been initialized
    if ( fullPay > 1000)
    ^
    1 error

  10. #10
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Ok, move the following line above the if statement:

    Java Code:
    fullPay = hoursWorked * payRate;

  11. #11
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    That fixed the error but it's still not listing the taxes out. I actually need lines to show federal, state and local. Do I need a line in the output section for that?


    Enter hours worked this week: 40
    Enter your weekley pay rate: 250
    Total hours entered =40
    Pay Rate entered =250.0

    Before taxes, your weekly pay is : 10000.0
    After taxes, your weekly pay is : 3500.0000000000005



    ----jGRASP: operation complete.

  12. #12
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Your weekly pay should be:

    Java Code:
    weeklyPay = fullPay * (1-totalTaxRate)
    What you are currently calculating is the tax.

  13. #13
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    My calculations are correct, but I actually need line items (see below) to show up when I run it. I have to have the taxes broken out separately not just figured in my net pay. I can't figure out how to make the taxes show up as their own line items.

    Enter hours worked this week: 40
    Enter your weekley pay rate: 250
    Total hours entered =40
    Pay Rate entered =250.0

    Federal Tax=
    State Tax=
    Local Tax=


    Before taxes, your weekly pay is : 10000.0
    After taxes, your weekly pay is : 3500.0000000000005


    Quote Originally Posted by shall View Post
    Your weekly pay should be:

    Java Code:
    weeklyPay = fullPay * (1-totalTaxRate)
    What you are currently calculating is the tax.

  14. #14
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Your code will be easier to read if you don't combine the output into one string. Also, you won't need all those "\n".

    Output them with separate System.out.println statements, i.e.

    Java Code:
    System.out.println( " Total hours entered =" + hoursWorked  );
    System.out.println( " Pay Rate entered =" + payRate );
    System.out.println(" Before taxes, your weekly pay is : " + beforeTaxPay);
    System.out.println(" After taxes, your weekly pay is : " + weeklyPay);

    Then, just add the new outputs for tax. I show one of them below.

    Java Code:
    double federaltax = fullPay * federalTaxRate;
    System.out.println("  Federal Tax : " + federaltax);

  15. #15
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    An example at Decimals in Java - CodingForums.com shows you how to format your numbers.

  16. #16
    thessler1 is offline Member
    Join Date
    Jun 2012
    Posts
    16
    Rep Power
    0

    Default Re: NetPay java programming error-Need help

    Thank you so much. My teacher hasn't been much help and the book is useless. It all works now.

Similar Threads

  1. Replies: 13
    Last Post: 05-24-2012, 10:50 AM
  2. Disproof to a conjecture or a programming error?
    By tbrowne03 in forum New To Java
    Replies: 5
    Last Post: 11-26-2011, 09:08 AM
  3. Socket Programming ERROR : Socket not connected
    By vishrut_n_shah in forum Networking
    Replies: 0
    Last Post: 11-04-2011, 10:32 AM
  4. Socket applet programming error?
    By Master Zero in forum Java Applets
    Replies: 6
    Last Post: 10-05-2011, 10:15 PM
  5. Replies: 16
    Last Post: 05-28-2009, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •