I have this code that I have been working on for a week now for my Java Programming class at ASU. It is a tax calculator that asks the user for their name, filing status, and annual income. The program then determines taxes to be paid based on the user's input.
If the user enters anything but the available options as their filing status, I have an error message that lets the user know their filing status input is invalid and to restart the program. When ran, however, once the user enters their annual income, the error message appears regardless of their choice of filing status. What have I done wrong?
import java.util.Scanner; public class TaxCalculator { public static void main(String[] args) { // Start input Scanner Scanner scanner = new Scanner(System.in); String name = ""; System.out.print("What is your name?: "); name = scanner.nextLine(); System.out.printf("\nHello, %s and welcome to the tax calculator.\n" + "Follow the prompts to determine total income tax for annual income.\n\n", name); // Prompt the user to enter filing status String status = ""; System.out.println( "SG - Single\n" + "MJ - Married, Filing Jointly\n" + "MS - Married, Filing Separately\n" + "HH - Head of Household\n\n"); System.out.print("Enter the two letter code which corresponds to your filing status: "); status = scanner.nextLine(); // Prompt the user to enter taxable income System.out.print("Enter your gross annual income: "); double income = scanner.nextDouble(); // Compute tax double tax = 0.0; // Tax calculation for Single filers if (status == "SG") { if (income < 30000) tax = ((income - 5950) * .025); else if (income >= 30000 && income < 60000) tax = (((income - 5950) * 0.10) + ((income - 5950) * 0.025)); else if (income >= 60000 && income < 100000) tax = (((income - 5950) * 0.20) + ((income - 5950) * 0.025)); else if (income >= 100000 && income < 250000) tax = (((income - 5950) * 0.30) + ((income - 5950) * 0.025)); else tax = (((income - 5950) * 0.40) + ((income - 5950) * 0.025)); } // Tax calculation for Married, Joint filers else if (status == "MJ") { if (income < 30000) tax = ((income - 11900) * .025); else if (income < 60000) tax = (((income - 11900) * 0.10) + ((income - 11900) * 0.025)); else if (income < 100000) tax = (((income - 11900) * 0.20) + ((income - 11900) * 0.025)); else if (income < 250000) tax = (((income - 11900) * 0.30) + ((income - 11900) * 0.025)); else tax = (((income - 11900) * 0.40) + ((income - 11900) * 0.025)); } // Tax calculation for Married, Separate filers else if (status == "MS") { if (income < 30000) tax = ((income - 5950) * .025); else if (income < 60000) tax = (((income - 5950) * 0.10) + ((income - 5950) * 0.025)); else if (income < 100000) tax = (((income - 5950) * 0.20) + ((income - 5950) * 0.025)); else if (income < 250000) tax = (((income - 5950) * 0.30) + ((income - 5950) * 0.025)); else tax = (((income - 5950) * 0.40) + ((income - 5950) * 0.025)); } //Tax calculation for Head of Household filers else if (status == "HH") { if (income < 30000) tax = ((income - 8700) * 0.025); else if (income < 60000) tax = (((income - 8700) * 0.10) + ((income - 8700) * 0.025)); else if (income < 100000) tax = (((income - 8700) * 0.20) + ((income - 8700) * 0.025)); else if (income < 250000) tax = (((income - 8700) * 0.30) + ((income - 8700) * 0.025)); else tax = (((income - 8700) * 0.40) + ((income - 8700) * 0.025)); } else { System.out.println("Error: invalid status. Restart program and enter the two letter code that corresponds to your filing status and provide your annual income."); } // Display tax calculation result System.out.println("\nTax to be collected is " + tax); scanner.close(); } }
Thanks!