Question: /* Program 4: Design and implement (source code) a program (nameit IncomeTax) that reads from the user annual income, as integer value, and calculates the
/* Program 4: Design and implement (source code) a program (nameit IncomeTax) that reads from the user annual income, as integer value, and calculates the income tax based on the tax table. The program output should include the entered annual income followed by the applied tax bracket and the tax amount.
When I check for annual income of $910,000, I receive the following error, see below. What does this mean?
Annual Income: $910000.00 Tax bracket: 35% Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Number at java.base/java.text.DecimalFormat.format(DecimalFormat.java:518) at java.base/java.text.Format.format(Format.java:158) at IncomeTax.main(IncomeTax.java:56)
------------------------------
import java.util.Scanner; import java.text.DecimalFormat;
public class IncomeTax { public static void main(String[] args) { double annIncome; Scanner input = new Scanner(System.in); System.out.print("Enter annual income: "); annIncome = input.nextInt(); DecimalFormat dollars = new DecimalFormat("$0.00"); if (annIncome <= 50000) { System.out.println("Annual Income: " + dollars.format(annIncome)); System.out.println("Tax bracket: 5% "); System.out.println("Tax due amount: " + dollars.format(annIncome * 0.05)); } else if (annIncome > 50000 && annIncome <= 200000) { System.out.println("Annual Income: " + dollars.format(annIncome)); System.out.println("Tax bracket: 10% "); System.out.println("Tax due amount: " + dollars.format(((50000 * 0.05) + (0.10*(annIncome - 50000))))); } else if (annIncome > 200000 && annIncome <= 400000) { System.out.println("Annual Income: " + dollars.format(annIncome)); System.out.println("Tax bracket: 15% "); System.out.println("Tax due amount: " + dollars.format((((50000 * 0.05) + (0.10 * 200000) + (0.15*(annIncome - 250000)))))); } else if (annIncome > 400000 && annIncome <= 900000) { System.out.println("Annual Income: " + dollars.format(annIncome)); System.out.println("Tax bracket: 25% "); System.out.println("Tax due amount: " + dollars.format(((50000 * 0.05) + (0.10 * 200000) + (0.15 * 400000) + (0.25*(annIncome - 650000))))); } else if (annIncome > 900000) { System.out.println("Annual Income: " + dollars.format(annIncome)); System.out.println("Tax bracket: 35% "); System.out.println("Tax due amount: " + dollars.format(((50000 * 0.05) + (0.10 * 200000) + (0.15 * 400000) + (0.25 * 900000) + String.format("%.2f",0.35*(annIncome - 1550000))))); } else System.out.println("Get a job!!!"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
