Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Tax Calculator code error when prompting user for annual income.

  1. #1
    Junior Member rprollin's Avatar
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tax Calculator code error when prompting user for annual income.

    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!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,316
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Tax Calculator code error when prompting user for annual income.

    The problem is probably because of the Scanner class methods' treatment of line end characters.
    https://christprogramming.wordpress....on-mistakes-1/
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member rprollin's Avatar
    Join Date
    Aug 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tax Calculator code error when prompting user for annual income.

    Thank you for the help Norm! Looks like I had a few mistakes. It was also pointed out to me that my use of the double equals sign was not appropriate in my If-statements.

    instead of (status == "SG"),

    I should have used

    (status.equals("SG"))

    All of these If-statements are a bit bulky too. Once I learn how to use a switch, I'll make some changes

    if (status.equals("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));
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,316
    Thanks
    53
    Thanked 2,391 Times in 2,345 Posts

    Default Re: Tax Calculator code error when prompting user for annual income.

    The switch statement won't be any use for finding values in different ranges. The current if/else if/else would be better
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Prompting user to open file, then printing in System.out using toUpperCase
    By Demias in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 15th, 2014, 03:56 AM
  2. help in how to add code to work with income tax calculation program
    By jayokayo69 in forum Object Oriented Programming
    Replies: 3
    Last Post: January 21st, 2014, 01:14 PM
  3. Compute federal personal income tax of U.s of 2009
    By pink.devil.tanha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 06:42 AM
  4. Tax Refund Calculator......Need help finishing this code!!
    By txaviermars in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 25th, 2012, 11:16 AM
  5. Not prompting string user input.
    By Abhi01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2012, 10:26 PM