Search code examples
javajcreator

Error with Income Tax program in Java using JCreator


I am getting an error that says one of the variables I have used are not initialized but upon reviewing the code I realize that I have in fact initialized the variable so I don't understand why I keep getting the error.

This is the error:

    error: variable tax might not have been initialized
avgtax = (tax/sal)*100;
          ^

This is the code:

import java.util.*;
public class Taxable {
  final static double first15=0.1;
  final static double next20=0.2;
  final static double over35=0.25;
  final static double tax_inc=0.8;
  public static void main(String[] args) {
    double sal, TaxInc;
    double tax, avgtax;
    Scanner in = new Scanner(System.in);
    System.out.printf("Enter Salary: ");
    sal = in.nextDouble();
    TaxInc = sal*tax_inc;
    if (TaxInc > 0 && TaxInc <= 15000)
      tax = TaxInc*first15;
    else if (TaxInc > 15000 && TaxInc <= 35000)
      tax = (15000 * first15) + ((TaxInc - 15000) * next20);
    else if (TaxInc > 35000)
      tax = (15000 * first15) + (20000 * next20) + ((TaxInc - 35000) * over35);
    else
      System.out.printf("\nInvalid Salary Figure.");
    avgtax = (tax/sal)*100;
    System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is:"
      + " %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
  }
}

Any help would be greatly appreciated.


Solution

  • This has some fixes to your code so it compiles and runs as expected

    public class Taxable {
        final static double first15=0.1;
    
        final static double next20=0.2;
        final static double over35=0.25;
        final static double tax_inc=0.8;
        public static void main(String[] args) {
            double sal;
            double taxInc;
            double tax = 0;
            double avgtax;
            Scanner in = new Scanner(System.in);
            System.out.printf("Enter Salary: ");
            sal = in.nextDouble();
            taxInc = sal*tax_inc;
            if (taxInc > 0 && taxInc <= 15000) tax = taxInc*first15;
            else if (taxInc > 15000 && taxInc <= 35000) tax = (15000 * first15) + ((taxInc - 15000) * next20);
            else if (taxInc > 35000) tax = (15000 * first15) + (20000 * next20) + ((taxInc - 35000) * over35);
            else System.out.printf("\nInvalid Salary Figure.");
            avgtax = (tax/sal)*100;
            System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is: %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
        }
    }
    

    However its not nicely structured ... I suggest reading Java naming convention and programming standards

    EDIT:

    Although you have accepted an answer, i suggest looking at this code sample, its written in slightly more readable way :)

    import java.util.Scanner;
    
    
    public class Taxable {
        private final static double first15 =0.1;   
        private final static double next20  =0.2;
        private final static double over35  =0.25;
        private final static double tax_inc =0.8;
    
        private double sal;
    
        private double taxInc;
        private double tax = 0;
        private double avgtax;
        public static void main(String[] args) {
            System.out.printf("Enter Salary: ");
            Scanner in = new Scanner(System.in);
    
            Taxable taxable = new Taxable();
            taxable.setSal(in.nextDouble());
            System.out.println(taxable.calculateTax());
        }
        private String calculateTax(){  
            setTaxInc(getSal()*tax_inc);
    
            if (getTaxInc() > 0 && getTaxInc() <= 15000){
                setTax(getTaxInc()*first15);
            }
            else if (getTaxInc() > 15000 && getTaxInc() <= 35000){
                setTax((15000 * first15) + ((getTax() - 15000) * next20));
            }
            else if (getTaxInc() > 35000){
                setTax((15000 * first15) + (20000 * next20) + ((getTax() - 35000) * over35)) ;
            }
            else {
                System.out.printf("\nInvalid Salary Figure.");
            }
            setAvgtax((getTax()/getSal())*100);
            String calculation = "\n At a salary of: " + getSal() + "\n Tax to be paid is: " + getTax() + "\n Average Tax Rate is: " + getAvgtax(); 
            return calculation;     
        }
        public double getSal() {
            return sal;
        }
        public void setSal(double sal) {
            this.sal = sal;
        }
        public double getTaxInc() {
            return taxInc;
        }
        public void setTaxInc(double taxInc) {
            this.taxInc = taxInc;
        }
        public double getTax() {
            return tax;
        }
        public void setTax(double tax) {
            this.tax = tax;
        }
        public double getAvgtax() {
            return avgtax;
        }
        public void setAvgtax(double avgtax) {
            this.avgtax = avgtax;
        }
    }