I am trying to print a table with a double method..yet not working

i can’t seem to make the table that i am trying to print stop at 60.000 as it was suposed to. the printing goes on forever witch makes me belive i have created an infinite for loop.. the ideia is to print a table that gives the tax for values bettewn 50000 and 60000 in 4 types of fillers and with increment of 50 for every new row…

public class FinantialAplicationTable {

    public static void main(String[] args) {
        int status=0;
        double taxableIncome=0;         

        System.out.printf("Taxable IncometSingletMarried Filling JointlytMarried Filling SepratlytHead Of Householdn");
        System.out.printf("                           or Qualifing Widowern");
        System.out.print("______________________________________________________________________________________________________n");

        printTable(status,taxableIncome);
    }

    public static double printTable(int status, double taxableIncome){
        double tax1,tax2,tax3,tax4;
        for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){         
            tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
            tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
            tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
            tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);

            if (taxableIncome>=50000 && taxableIncome<=60000){
                System.out.println(Math.round(taxableIncome)+"  "+Math.round(tax1)+"  "+Math.round(tax2)+"  "+Math.round(tax3)+"  "+Math.round(tax4));
            }
        }
        return printTable(status,taxableIncome);
    }

} 

any help will be most welcome.

thank you in advance…

Answer

The reason that you print forever is because you don’t have an exit check. You have a recursive function that doesn’t give yourself a way out. To exit at 60 you could do:

  public static void printTable(int status, double taxableIncome){
        double tax1,tax2,tax3,tax4;
        for ( taxableIncome=50000;taxableIncome<60000;taxableIncome =taxableIncome+50){         
            tax1 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
            tax2 = 16700*0.10+(taxableIncome-16700)*0.15;
            tax3 = 8350*0.10+(33950-8350)*0.15+(taxableIncome-33950);
            tax4 = 11950*0.10+(45500-11950)*015+(taxableIncome-45500);

            if (taxableIncome>=50000 && taxableIncome<=60000){
                System.out.println(Math.round(taxableIncome)+"  "+Math.round(tax1)+"  "+Math.round(tax2)+"  "+Math.round(tax3)+"  "+Math.round(tax4));
            }
        }

    }

You change the return type from double to void because you don’t need to return anything. By returning the function, you will continue to run until you run out of memory.

Leave a Reply

Your email address will not be published. Required fields are marked *