-1
public class exerciseFive {

    public static void main (String[] args) {
        Scanner scanner = new Scanner(System.in);

        double finalTax = 0.0;
        char taxGroup = 0;
        int taxIncome = 0;
        int income = 0;
        int deduction = 0;
        int amount;
        do {
            System.out.println("Enter next amount: ");
            amount = scanner.nextInt();
            if (amount > 0) {
                income += amount;
            }
            else if (amount < 0) {
                deduction -= amount;
            }

        } while (amount != 0);

        System.out.println("Income      = $" + income);
        System.out.println("Deductions  = $" + deduction);
        System.out.println("Taxable Income  = $" + taxableIncome(income, deduction, taxIncome));
        System.out.println("Tax Group   = " + taxGroup(taxIncome, taxGroup));
        //System.out.println("Tax Owed  = $" + computeTax(taxGroup, taxIncome, finalTax));
    }

    public static int taxableIncome (int income, int deduction, int taxIncome) {
        taxIncome = income - deduction;
        if (taxIncome <= 0) {
            taxIncome = 0;
        }
        return taxIncome;
    }


public static char taxGroup (int taxIncome, char taxGroup) {


        if (taxIncome >= 500_000) {
            taxGroup = 'S';
        }
        else if (taxIncome >= 200_000 && taxIncome < 500000) {
            taxGroup = 'Q';
        }
        else if (taxIncome >= 100_000) {
            taxGroup = 'M';
        }
        else if (taxIncome >= 50_000) {
            taxGroup = 'A';
        }
        else if (taxIncome >= 20_000) {
            taxGroup = 'R';
        }
        else {
            taxGroup = 'P';
        }
        return taxGroup;
    }




My output:

Income      = $228000
Deductions  = $3450
Taxable Income  = $224550
Tax Group   = P

Correct Output:

Income         = $228000.0
Deductions     = $3450.0
Taxable income = $224550.0
Tax group      = Q

I believe the issue is in this method. I pass the char taxGroup as an argument in the taxGroup method but it is not being updated by the for-loop. I think it may have something to do with the variable being declared in the main method?? Thanks in advance for any help.

As you can see from the output my income, deductions, and taxable income are all correct. The only thing that is wrong is the Tax group.

  • Because `taxIncome` isn't being updated. See linked duplicate for actual reason(s). – Elliott Frisch May 13 '20 at 19:57
  • `taxIncome` variable that you are sending to the `taxGroup` method is not updated with the new value. Print the `taxIncome` in the `taxGroup` method and you'll see the value for `taxIncome`. In general it is bad practice to mix local,global variable names and method names. – varijkapil13 May 13 '20 at 20:00

1 Answers1

0

You need to store the result of taxableIncome() function in the variable taxIncome. So that your call to the function taxGroup() has the newly calculated taxIncome. Currently taxIncome has still a value of 0.

Kerem
  • 11,377
  • 5
  • 59
  • 58