-2

Right now, I am working on an exercise that requires me to use several different methods to and call all of them in the main method. This code specifically asks for a user's filing status and yearly income to calculate the user's deduction amount, tax rate, taxable income, and tax amount in their own seperate methods. Then in the main method, I need to call these methods and print out the value they return based on what the user has inputed. This is what I have so far:

import java.util.*;

public class TaxActivity{
 public static double getDeductionAmount(double filingStatus){
    Scanner sc = new Scanner(System.in);
    int deductionAmount;
    if (filingStatus==1){
        deductionAmount=6350;
    }else if(filingStatus==2){
        deductionAmount=12700;
    }else{
        deductionAmount=9350;
    }
    return deductionAmount; 
 }
 public static double getTaxBracket(double filingStatus, double yearlyIncome){
    Scanner sc = new Scanner(System.in);
    double taxRate;
    if (filingStatus==1){
        if (yearlyIncome>=0&&yearlyIncome<9325){
        taxRate=10.0;
        }else if(yearlyIncome>=9325&&yearlyIncome<37950){
        taxRate=15.0;
        }else if(yearlyIncome>=37950&&yearlyIncome<91900){
        taxRate=25.0;
        }else if(yearlyIncome>=91900&&yearlyIncome<191650){
        taxRate=28.0;
        }else if(yearlyIncome>=191650&&yearlyIncome<416700){
        taxRate=33.0;
        }else if(yearlyIncome>=416700&&yearlyIncome<418400){
        taxRate=35.0;
        }else{
        taxRate=39.6;
        }
    }else if(filingStatus==2){
        if (yearlyIncome>=0&&yearlyIncome<18650){
        taxRate=10.0;
        }else if(yearlyIncome>=18650&&yearlyIncome<75900){
        taxRate=15.0;
        }else if(yearlyIncome>=75900&&yearlyIncome<153100){
        taxRate=25.0;
        }else if(yearlyIncome>=153100&&yearlyIncome<233350){
        taxRate=28.0;
        }else if(yearlyIncome>=233350&&yearlyIncome<416700){
        taxRate=33.0;
        }else if(yearlyIncome>=416700&&yearlyIncome<470700){
        taxRate=35.0;
        }else{
        taxRate=39.6;
        }
    }else{
        if (yearlyIncome>=0&&yearlyIncome<13350){
        taxRate=10.0;
        }else if(yearlyIncome>=13350&&yearlyIncome<50800){
        taxRate=15.0;
        }else if(yearlyIncome>=50800&&yearlyIncome<131200){
        taxRate=25.0;
        }else if(yearlyIncome>=131200&&yearlyIncome<212500){
        taxRate=28.0;
        }else if(yearlyIncome>=212500&&yearlyIncome<416700){
        taxRate=33.0;
        }else if(yearlyIncome>=416700&&yearlyIncome<444550){
        taxRate=35.0;
        }else{
        taxRate=39.6;
        }
    }
    return taxRate;
 }
 public static double getTaxableIncome(double deductionAmount, double yearlyIncome){
    Scanner sc = new Scanner(System.in);
    double taxableIncome= yearlyIncome - deductionAmount;
    return taxableIncome;
 }
 public static double getTaxAmount(double taxableIncome, double taxRate){
        double taxAmount=(double)taxableIncome*taxRate;
        return taxAmount;
 }
 public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter your filing status: ");
    System.out.println("1. Single ");
    System.out.println("2. Married Filing Jointly ");
    System.out.println("3. Head of Household ");
    System.out.println("");
    System.out.print("Enter either 1, 2, or 3: ");
    double filingStatus = sc.nextDouble();
    System.out.println("");

    System.out.println("Enter your yearly income: ");
    double yearlyIncome = sc.nextDouble();
    System.out.println("");

    System.out.println("Your Deduction Amount is: $"+getDeductionAmount(filingStatus));
    System.out.println("Your Tax Rate is: "+getTaxBracket(filingStatus,yearlyIncome)+"%");
    System.out.println("Your Taxable Amount is: $"+getTaxableIncome(deductionAmount,yearlyIncome));
    System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));
 }
}

However, when I run the code, I get these errors:

TaxActivity.java:96: error: cannot find symbol
            System.out.println("Your Taxable Amount is: $"+getTaxableIncome(deductionAmount,yearlyIncome));

symbol:   variable deductionAmount
location: class TaxActivity



TaxActivity.java:97: error: cannot find symbol
            System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));

symbol:   variable taxableIncome
location: class TaxActivity



TaxActivity.java:97: error: cannot find symbol
System.out.println("Your Amount of Tax Due is: $"+getTaxAmount(taxableIncome,taxRate));

symbol:   variable taxRate
location: class TaxActivity
3 errors

I'm still beginning in Java, so calling methods is still a new concept to me, but I can't seem to see what could be causing these errors. I made sure to make all the variables the same type, and used the right parameters for all of the methods. The only thing I can think of that might be causing problems is an error in the code's formatting that I am unable to notice. I'd appreciate it if any of you could point out what could be causing this error.

yassadi
  • 524
  • 1
  • 9
  • 20
  • Have you seen this?https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean – yassadi Apr 11 '18 at 04:03
  • `getTaxableIncome(deductionAmount,yearlyIncome));` is a compile error because you never define the variable `deductionAmount` inside the scope `main`. – MFisherKDX Apr 11 '18 at 04:07
  • Your code is uncompilable. You will need to declare the variables `deductionAmount` and `taxableIncome` and assign values to them – Mohit Mutha Apr 11 '18 at 04:10

2 Answers2

1

You can't access variables in one method that were created in another method.

Your main method has the line:

System.out.println("Your Taxable Amount is: $"+getTaxableIncome(deductionAmount,yearlyIncome));

But there is no variable deductionAmount in your main method. You did run the function to compute it, but you never saved it to a variable in your main method. You should have done:

int deductionAmount = getDeductionAmount(filingStatus);

Similarly with taxableIncome.

jojois74
  • 795
  • 5
  • 10
0

You should store return value of your getDeductionAmount(filingStatus) method In your main method.
like float deductionAmount = getDeductionAmount(filingStatus) then you have to pass your deduction amount to getTaxableIncome(deductionAmount,yearlyIncome) method.

Similarly you should store taxable income and taxRate also.
Then do what ever you want with that variables like printing and passing to other methods.

Pavan
  • 103
  • 1
  • 6