hi everyone
i havent been here in a long while since i have been here anyway, i am doing a program(practicing) but i dont or cant say if it is my calculation is off
everything is working properly except for the the income tax part. it is outputting zero. tell me what is wrong with my calculation and how do i go about fixing it. thanks much in advance.

 final double health_surcharge = 33.50;

        double hrs, hrswrk, gsalary = 0, NIS,nsalary=0, rate_of_pay = 0,totaldeduct = 0, incometax, teaching_allow;
        double overtime = 0, income_tax = 0, total_tax;


        System.out.println("please enter the lecturer id number");
        String lecturerID = userinput.nextLine();

        System.out.println("please enter the lecturer first name");
        String firstname = userinput.next();

        System.out.println("please enter the lecturer last name");
        String lastname = userinput.next();

        System.out.println("please enter the number of hours work");
        hrswrk = userinput.nextDouble();

        System.out.println("please enter the teaching allowance");
        teaching_allow = userinput.nextDouble();

        System.out.println("please enter your NIS contribution");
        NIS = userinput.nextDouble();


        hrs = (hrswrk * rate_of_pay) + teaching_allow;
        hrs = (overtime * rate_of_pay);
        total_tax = (health_surcharge + NIS) + (gsalary * 0.5);
        total_tax = (gsalary * 0.5);
        gsalary = (gsalary + teaching_allow);
        totaldeduct =(NIS + health_surcharge);
        nsalary = (gsalary - totaldeduct);


        System.out.println("the lecturer id is "+ lecturerID);
        System.out.println("the lecturer first and last name is "+ firstname +lastname);
        System.out.println("the hours worked is "+ hrswrk);
        System.out.println("the income tax is "+income_tax);
        System.out.println("the total deduction is "+totaldeduct);
        System.out.println("the gross salary is "+gsalary);
        System.out.println("the net salary is "+nsalary);

Dani AI

Generated

Core issue: the income_tax variable is never assigned a computed value, so it stays zero. pointed this out; rightly recommended clearer names and a paper-and-pencil walkthrough. Beyond that, the code overwrites values and computes items in the wrong order (variables used before being set, duplicate assignments to the same variable). Fix those two things first and the tax will stop printing as zero.

Practical checklist to apply in order:

  • Give variables clear roles: hoursWorked, hourlyRate, overtimeHours, teachingAllowance, grossSalary, incomeTax, totalDeductions, netSalary.
  • Read and validate inputs (make sure hourlyRate and hours are not zero or missing).
  • Compute gross salary first, then compute tax from that gross using an explicit taxRate (or tax-bracket logic).
  • Compute deductions (NIS, health surcharge) and subtract tax + deductions to get net salary.
  • Don’t reuse or overwrite a variable for a different meaning (e.g., don’t use an “hours” var to hold money).

Example pattern (replace the example values and taxRate with the real business rules):

double hourlyRate = 25.00;       // obtain from input
double hoursWorked = 40.0;
double overtimeHours = 5.0;
double teachingAllowance = 200.0;
double healthSurcharge = 33.50;
double nis = 50.00;

double grossSalary = (hoursWorked * hourlyRate) + (overtimeHours * hourlyRate) + teachingAllowance;
double taxRate = 0.12;           // example: 12% — change to required rate/brackets
double incomeTax = grossSalary * taxRate;
double totalDeductions = nis + healthSurcharge + incomeTax;
double netSalary = grossSalary - totalDeductions;

System.out.printf("Gross: %.2f  Tax: %.2f  Deductions: %.2f  Net: %.2f%n",
                  grossSalary, incomeTax, totalDeductions, netSalary);

Quick debugging tips: print intermediate values (grossSalary, taxRate, incomeTax) to find where zero first appears; check that you initialized hourlyRate (a common cause of gross == 0); avoid mixing next()/nextLine() without consuming the leftover newline; and remove the duplicate/incorrect total_tax assignments (one was probably intended to set incomeTax). Following ’s stepwise approach—code one small calculation, verify it on paper, then proceed—prevents these errors.

Recommended Answers

All 5 Replies

You never assign income_tax a value other than 0 when you declare it. You do, however, calculate total_tax twice. Should one of those possibly be income_tax instead?

i am totally lost at what value ah should put.

Typically your income_tax will be some percentage of another value. Figure out what that other value is and calculate a percentage of it.

I'm sorry, but lines 26-32 are complete nonsense. Just delete them and start again by doing the following:
1. Keep a copy of the requirements in front of you at all time - they should detail the calculations to be done.
2. Give your variables descriptive accurate names - eg you have a var hrs that looks like it holds a number of hours, but actually seems to hold monetary amounts. Name them so it's explicit whether a var is a number of hours, a percentage or a monetary amount. That will help avoid fundamental errors like adding together a percentage and a quantity.
3. Get a sheet of paper. write down all your vars with a space to write their values. Write down a typical test case.
4. Try to code the very first step of the calculation. Pretend you are the computer and use your sheet of paper to follow the instructions in that line of code.
5. If/when that seems to make sense, go on to the next line.
6. Repeat until the calculations are complete and your sheet of paper is showing a complete set of correct values.
7. Remember the take-away moral, which is that if you can't run through your calculations on a sheet of paper then you certainly have no idea how to code them as a program.

If that seems too tedious, you can always use the Alternative Approach:
1. Take a few random guesses as to what code might be correct
2. Watch it fail to compile or execute with incorrect results.
3. Throw hands up in despair

:) JC

commented: Hah! +14

I will try that james and thank you reverend jim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.