Hi there everyone, I have a bit of a problem I am hoping you can help with. This is for an assignment, and I know I can finish the final calculations for my tax program if I could just pass my 2 variables from previous methods into the final method to get my total taxable income. Here is what I have so far which seems to work, I am not sure though how to correctly enter the parameters for the last method to pull the variables I need through:

import java.util.*;
public class Tax {
    static Scanner input = new Scanner (System.in);
    public static void main(String[] args) {

        System.out.println("Welcome to Tax Calculator");
        getIncome();
    }

    public static double getIncome(){

        double income = 0;
        System.out.println("Please enter income: ");
        double i = input.nextDouble();
        income = i;

        while (i >0){

        System.out.println("Please enter income: ");
        i = input.nextDouble();
        income += i;

        if (i < 0){
            income = (income-i);
            System.out.println("Total income entered is: " + income);
            getExpenses();
        }
        }
        return income;

    }
    public static double getExpenses(){
        double expenses = 0;
        System.out.print("Please enter expenses: ");
        double j = input.nextDouble();
        expenses = j;

        while (j > 0){

            System.out.print("Please enter expenses: ");
            j = input.nextDouble();
            expenses += j;

        if (j < 0){
            expenses = (expenses-j);
            System.out.println("Total expenses entered: " + expenses);

            calculateTaxPayable(income, expenses);
        }
        }

        return expenses;


    }
    public static double calculateTaxPayable(double income, double expenses){

        double taxable = (income - expenses);
        System.out.println("Total taxable income: " + taxable);
        return taxable;
    }

}

Any help would be greatly appreciated! In the calculateTaxPayable() method, I need to pull through income from getIncome() and expenses from getExpenses() and then do some calculations as to how much tax the user should pay. Thanks very much in advance

The strangest thing about the code you have now is that you are ignoring the return value of getIncome and getExpenses. Based on the names of those methods, people would expect they are invoked mostly for their return value, so it is confusing that you aren't using the return value.

If it were my project, I would make my main look more like this:

public static void main(String[] args) {
    System.out.println("Welcome to Tax Calculator");
    double income = getIncome();
    double expenses = getExpenses();
    System.out.println("Total taxable income: " + calculateTaxPayable(income, expenses));
}

Of course you would need to modify getIncome and getExpenses to make it work that way. You shouldn't be calling getExpenses in getIncome anyway, since expenses are not part of income.

Aside from that, money should never be represented as a double. A double is an imprecise value and pennies tend to get lost or magically appear when you do arithmetic on money that way. If this project is serious then that would be a very serious problem.

Hi bguild, thanks for the quick reply. I now feel quite lost though. Am I right in saying that I could remove the double & return statement from the methods and then call the variables in the calculateTaxPayable method parameters? As for using doubles, it is in the assignment spec so I am stuck with that one :)

So if I make the return type void and not call getExpenses from getIncome, I can control it through the main method? Thanks again for the help

I now feel quite lost though. Am I right in saying that I could remove the double & return statement from the methods and then call the variables in the calculateTaxPayable method parameters?

I think the answer is yes, if you mean changing getExpenses() into getExpenses(double income). That would do what you need, but doing it that way is unusual. If I were evaluating an assignment that did that, I would probably decide that the person who wrote it doesn't understand return values.

A return value is traditionally how a method reports the result of whatever it does. getIncome should return the income and that returned value should be used, not ignored. And the same with getExpenses and probably calculateTaxPayable, too.

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.