0

The Question:

Write a program that prompts the user to enter his/her taxable income and then calculates the income tax due using the 2014 tax table below. Express the tax with two decimal places.

Income tax brackets for single-filers
up to $9075          10%
$9076 - $36900    15%
$36901 - $89350       25%
$89351 - $186350      28%
$186351 - $405100   33%

This is what I have so far. I'm really new to Java so keep that in mind. My specific question will come after my code.

import java.util.Scanner;
public class IncomeTax {    


public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Prompt the user to enter taxable income
        System.out.print("Enter the amount of taxable income for the year 2014: ");
        double income = input.nextDouble();

        // Compute tax
        double tax = 0;

        if (income <= 9075)
            tax = income * 0.10;
        else if (income <= 9076)
            tax = 9075 * 0.10 + (income - 36900) * 0.15;
        else if (income <= 36901)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (income - 89350) * 0.25;
        else if (income <= 89351)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (income - 186350) + 0.28;
        else if (income <= 186351)
            tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (89351 - 186350) + 0.28 + (income - 405100) + 0.33;

        if (income <=  9075)
            System.out.println("You have entered the 10% bracket.");
        else if (income <= 9076)
            System.out.println("You have entered the 15% bracket.");
        else if (income <= 36901)
            System.out.println("You have entered the 25% bracket.");
        else if (income <= 89351)
            System.out.println("You have entered the 28% bracket.");
        else if (income <= 186351)
            System.out.println("You have entered the 33% bracket.");
    }
}

The final output should look like this:

Enter the amount of taxable income for the year 2014. (user input here ->) 5000

You have entered the 10% tax bracket.

Your income is $5,000.00, Your tax is: $500.00. Your income after tax is: $4,500.00


How do I get my output to look like the above output? Specifically the decimals and the $ sign.

The output code that I am currently working on is:

System.out.println("Your income is: " + "Your taxx is: " + (int)(tax * 100) / 100.0) + "Your income after tax is: " + ;
Michael
  • 63
  • 2
  • 2
  • 6
  • 4
    for **Your income is $5,000.00, Your tax is: $500.00. Your income after tax is: $4,500.00** Where is the snippet of code that does this ? – Abdelrahman Elkady Mar 28 '15 at 20:48
  • That's the answer that my professor said the program should end with if I were to input 5000. I specifically said what it should look like and what I am getting. – Michael Mar 28 '15 at 20:50
  • That seems to be correct. You calculate tax but never output it. –  Mar 28 '15 at 20:50
  • How do I have my output give me an answer in that decimal and money form? – Michael Mar 28 '15 at 20:51
  • I'm trying to figure out how to get my output to look like the correct answer which is given up above. – Michael Mar 28 '15 at 20:54

3 Answers3

5

Simply append the following part to the code:

DecimalFormat formatter = new DecimalFormat("###,###,###.00");
System.out.println("Your inccome is $"+formatter.format(income)+", your tax is: $"+formatter.format(tax)+". Your income after tax is: $"+formatter.format(income-tax));

Here the DecimalFormatter is a technique to format numbers properly (for instance with commas separating the thousands, and two digits after the decimal dot).

But your code is quite sloppy and error-prone and sometimes doesn't seem to make much sense. You can better subdivide the calculation of taxes using different brackets:

double[] max = {0,9075,36900,89350,186350,405100};
double[] rate = {0,0.10,0.15,0.25,0.28,0.33};
double left = income;
double tax = 0.0d;
for(int i = 1; i < max.length && left > 0; i++) {
    double df = Math.min(max[i]-max[i-1],left);
    tax += rate[i]*df;
    left -= df;
}
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I am currently in chapter 3 of my book and have not learned many proper techniques such as that bracket method. Thank you for the suggestion though! – Michael Mar 28 '15 at 20:59
  • When using the DecimalFormatter where would I put that in my code? I have it towards the top under my public static void. – Michael Mar 28 '15 at 21:11
  • I'm getting the output: Your income is: $5,000, your tax is: $500. Your income after tax is: $4,500 Edit: fixed it by adding .00 in the DecimalFormatter. – Michael Mar 28 '15 at 21:12
  • @Michael: you probably copied the old version it's `###,###,###.00`, not `###,###,###.##`... – Willem Van Onsem Mar 28 '15 at 21:16
3

All you need to do is print the tax. You can format it with NumberFormat, as mentioned here:

NumberFormat format = NumberFormat.getCurrencyInstance();

System.out.println("You have entered the 10% bracket.");
System.out.println("Your income is $"+format.format(income)+". Your tax is $"+tax+". Your income after tax is "+format.format(income-tax)+".");
Community
  • 1
  • 1
KSFT
  • 1,774
  • 11
  • 17
2

simplest way to use DecimalFormat

you will need to import java.text.DecimalFormat

DecimalFormat formatter = new DecimalFormat("#,###.00");
System.out.println(formatter.format(1122334455));

will output :

1,122,334,455.00

Abdelrahman Elkady
  • 2,518
  • 2
  • 21
  • 30