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 6 of 6

Thread: wont get right output

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default wont get right output

    //program to taxreturn

    import java.util.Scanner;

    public class TaxReturn

    {
    public static void main(String[] args)
    {

    int filingStatus;
    double personalIncome;
    double tax=0;

    //Declare Scanner
    Scanner input = new Scanner(System.in);

    System.out.println ("1 - Single");
    System.out.println ("2 - Married Filing Jointly");
    System.out.println ("3 - Married Filing Separately");
    System.out.println ("4 - Head of Household");
    System.out.println();

    System.out.print("Please enter your filing status : ");
    filingStatus = input.nextInt();

    System.out.print("Please enter your personal income : ");
    personalIncome = input.nextInt();

    switch(filingStatus)
    {
    case 1:
    if(personalIncome > 372950)
    {
    tax = tax+(33/100)*372950 + ((personalIncome-372950)*(35/100));
    }
    else if(personalIncome>171550 && personalIncome<= 372950)
    {
    tax = tax+(28/100)*171550 + ((personalIncome-171550)*(33/100));
    }
    else if(personalIncome>82250 && personalIncome<= 171550)
    {
    tax = tax+(25/100)*82250 + ((personalIncome-82250)*(28/100));
    }
    else if(personalIncome>33950 && personalIncome<= 82250)
    {
    tax = tax+(15/00)*33950 + ((personalIncome-33950)*(25/100));
    }
    else if(personalIncome>8350 && personalIncome<=33950)
    {
    tax = tax+(10/100)*8350 + ((personalIncome-8350)*(15/100));
    }
    else
    {
    tax = tax + (10/100)*personalIncome;
    }
    break;

    case 2:
    if(personalIncome >372950)
    tax = tax + (33/100)*372950 + ((personalIncome-372950)*(35/100));
    else if(personalIncome>208850 && personalIncome<= 372950)
    tax = tax + (28/100)*208850 + ((personalIncome-208850)*(33/100));
    else if(personalIncome>137050 && personalIncome<= 208850)
    tax = (25/100)*137050 + ((personalIncome-137050)*(28/100));
    else if(personalIncome>67900 && personalIncome<= 137050)
    tax = tax + (15/00)*67900 + ((personalIncome-67900)*(25/100));
    else if(personalIncome>16700 && personalIncome<=67900)
    tax = tax + (10/100)*16700 + ((personalIncome-16700)*(15/100));
    else
    tax = tax + (10/100)*personalIncome;

    break;

    case 3:
    if(personalIncome > 186475)
    tax = tax + (33/100)*186475 + ((personalIncome-186475)*(35/100));
    else if(personalIncome>104426 && personalIncome<= 186475)
    tax = tax + (28/100)*104426 + ((personalIncome-104426)*(33/100));
    else if(personalIncome>68526 && personalIncome<= 104425)
    tax = tax + (25/100)*68526 + ((personalIncome-68526)*(28/100));
    else if(personalIncome>33950 && personalIncome<= 68525)
    tax = tax + (15/00)*33950 + ((personalIncome-33950)*(25/100));
    else if(personalIncome>8350 && personalIncome<=33950)
    tax = tax + (10/100)*8350 + ((personalIncome-8350)*(15/100));
    else
    tax = tax + (10/100)*personalIncome;

    break;
    case 4:
    if(personalIncome > 372950)
    tax = tax + (33/100)*372950 + ((personalIncome-372950)*(35/100));
    else if(personalIncome>190200 && personalIncome<= 372950)
    tax = tax + (28/100)*190200 + ((personalIncome-190200)*(33/100));
    else if(personalIncome>117450 && personalIncome<= 190200)
    tax = tax + (25/100)*117450 + ((personalIncome-117450)*(28/100));
    else if(personalIncome>45500 && personalIncome<= 117450)
    tax = tax + (15/00)*45500 + ((personalIncome-45500)*(25/100));
    else if(personalIncome>11950 && personalIncome<=45500)
    tax = tax + (10/100)*11950 + ((personalIncome-11950)*(15/100));
    else
    tax = tax + (10/100)*personalIncome;

    break;

    default:
    System.out.print("Invalid entry");
    break;


    } //End switch

    //Display the output

    if (filingStatus >= 1 && filingStatus<=4)
    {
    System.out.printf("The tax is %f", tax);

    }
    else
    System.out.println("Invalid entry");

    }

    }

    when I run the program I get my tax return 0.000.......I don't know what is the problem
    Could someone help me please?


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: wont get right output

    For future reference use tags [code] Post code in side tags [/code]

    System.out.println(); is a great way to check what is happening throughout your code. You can use it to check where your values are suppose to change and see if they are getting assigned correctly. Now you say your tax return is 0.00000 What inputs have you tried? Does it show for all of them or just for certain inputs? Also from how your code is it looks like it will show 2 "Invalid entry" when (1 > filingStatus > 4)

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: wont get right output

    Quote Originally Posted by Ubiquitous View Post
    For future reference use tags [code] Post code in side tags [/code]

    System.out.println(); is a great way to check what is happening throughout your code. You can use it to check where your values are suppose to change and see if they are getting assigned correctly. Now you say your tax return is 0.00000 What inputs have you tried? Does it show for all of them or just for certain inputs? Also from how your code is it looks like it will show 2 "Invalid entry" when (1 > filingStatus > 4)


    Thank you for your reply......

    I tried checking value using System.out.printf("%f",tax); and in all the place it shows 0.0000
    I don't know what is the problem...This is my second time using switch statement.....I don't know if I can use multiple if else statement inside switch..........and also I tried with multiple values to check the tax......Every time it gives me 0.0000....... Is there any arithmetic error?

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: wont get right output

    You are doing Integer division as far as I can tell in all you tax calculations:

    tax = tax+(33/100)*372950 + ((personalIncome-372950)*(35/100));

    should look like this tax = tax+(33.0/100)*372950 + ((personalIncome-372950)*(35/100));

    By adding the .0 you are wont lose all your extra data that integer division gets rid of.
    I haven't checked if its returning the correct answer but that did clear up the problem of receiving 0 as a answer when I tested it

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: wont get right output

    Quote Originally Posted by Avatar19 View Post
    You are doing Integer division as far as I can tell in all you tax calculations:

    tax = tax+(33/100)*372950 + ((personalIncome-372950)*(35/100));

    should look like this tax = tax+(33.0/100)*372950 + ((personalIncome-372950)*(35/100));

    By adding the .0 you are wont lose all your extra data that integer division gets rid of.
    I haven't checked if its returning the correct answer but that did clear up the problem of receiving 0 as a answer when I tested it

    Thank you very much......it worked once i added .00...........You are a life saver.....thank you very much once again

  6. #6
    Junior Member
    Join Date
    May 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: wont get right output

    Cool, no problem

Similar Threads

  1. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  2. CardLayout wont go to the next card
    By pottsiex5 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 25th, 2011, 04:04 PM
  3. WHY WONT THIS COMPILE!
    By usmc0311 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 12th, 2011, 02:42 PM
  4. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  5. pictures wont load
    By wolfgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 09:34 AM