Calculate federal taxes program! Help!

• March 10th, 2010, 12:10 AM
ocmjt
Calculate federal taxes program! Help!
Hello everyone! My names Mary and I'm very new to java and computer programming in general, taking my first class at school right now. We have been asked to write a java program that calculates federal taxes. It has to calculate the appropriate tax for each amount and keep a total of all the taxes. It has to display the tax for each income then show the sum at the end. I have to use a -1 number to tell the program to end.

I think that I could do this if the taxrate was one number but the tricky part for me but probably the easy part for you to understand is that a person pays 10% for the first 15k..if it is over 15k they pay 10% for the first 15k and 15% for the next 45k. If it is over 60k they pay what I just said and 33% for the income over 33%.

Can someone please help me figure this out! I have no idea how to incorporate that last part so if someone could show me how to do this program I would greatly appreciate it! And please explain a little bit what your doing so I can learn from it...I know I have to use a while loop to end the program with a negative number which I think I know how to do. But I also know I have to use a if statement and constants which I do not understand how to do. Thankyouuuu:)>-
• March 10th, 2010, 02:18 AM
ocmjt
Re: Calculate federal taxes program! Help!
This is what I have so far..I think I am close someone please help? I have errors under income and tax and it says I should make them say =0 next to the double but I dont think thats right?

Code :

``` public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = .1; final double MEDTAXRATE = .15; final double HIGHTAXRATE = .33; double sumOfTax=0; double income; double tax; System.out.println("Please input the income, type -1 to end");   while(income>=0) { sumOfTax = sumOfTax + tax; System.out.print("Please input the income, type -1 to end"); income = keyboard.nextDouble(); tax = keyboard.nextDouble();   if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000*HIGHTAXRATE); else tax=1500+(income-15000*MEDTAXRATE);   } tax = keyboard.nextDouble(); System.out.printf("The tax is %.2f%%", tax); System.out.printf("The sumOfTax is %.2f%%", sumOfTax);         }   }```
• March 10th, 2010, 10:06 AM
helloworld922
Re: Calculate federal taxes program! Help!
You need to initialize all variables before you can use them. This is different from some programming languages which initialize their variables to a "default" value. They don't necessarily need to be initialized to 0, but Java suggest 0 because that's what some other programming languages define as the default value. You also don't need to initialize variables right away, but it is generally a good idea to.
You can initialize your variables like so:

Code :

``` System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); // using your Scanner object to get the income value```

Later on in your code, you try to read in the tax value from your Scanner object. This is not what you want since the tax rate should be calculated based on the income the user typed in. Simply remove all lines that look like this:
Code :

`tax = keyboard.nextDouble();`
• March 10th, 2010, 01:45 PM
ocmjt
Re: Calculate federal taxes program! Help!
Thank you for your help helloworld..I deleted all the tax=keyboard.nextDoubles but if I do that I have to do double tax=0; at the top?
This is what I have now and nothing even comes up when I run it? Can someone please tell me what I am doing wrong? I have to turn in this program by midnight!

Code :

``` public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = .1; final double MEDTAXRATE = .15; final double HIGHTAXRATE = .33; double sumOfTax=0; double income = keyboard.nextDouble(); double tax=0;     while(income>=0) { sumOfTax = sumOfTax + tax; System.out.print("Please input the income, type -1 to end"); income = keyboard.nextDouble(); System.out.printf("The tax is %.2f%%", tax);   if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000*HIGHTAXRATE); else if(income<=60000&&income>15000) tax=1500+(income-15000*MEDTAXRATE);   }   System.out.printf("The tax is %.2f%%", tax); System.out.printf("The sumOfTax is %.2f%%", sumOfTax);         }   }```
• March 10th, 2010, 04:41 PM
helloworld922
Re: Calculate federal taxes program! Help!
What your program is doing is waiting for you to enter an initial double. What you should be doing is displaying the text for entering the income before asking for the income.
Code :

```System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble();```

• March 10th, 2010, 08:44 PM
ocmjt
Re: Calculate federal taxes program! Help!
Alright I added that and it asked the question now but now answer comes up when I turn it in. Please someone tell me what is wrong with this code? Thankyouuuuuu!!!!

Code :

```public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = 0.1; final double MEDTAXRATE = 0.15; final double HIGHTAXRATE = 0.33; double sumOfTax=0; System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); double tax=0;     while(income>=0) { sumOfTax = sumOfTax + tax; System.out.print("Please input the income, type -1 to end"); income = keyboard.nextDouble();       if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000*HIGHTAXRATE); else if(income<=60000&&income>15000) tax=1500+(income-15000*MEDTAXRATE);   }   System.out.printf("The tax is %.2f%%", tax); System.out.printf("The sumOfTax is %.2f%%", sumOfTax);         }   }```
• March 10th, 2010, 09:08 PM
ocmjt
Re: Calculate federal taxes program! Help!
but no answer comes up when i enter the income*...Can someone help me, I need it to tell me the tax every time and then the sum of the taxes at the end when I hit -1. Thanks!
• March 10th, 2010, 09:47 PM
ocmjt
Re: Calculate federal taxes program! Help!
Ok I have messed with the calculations and they work correctly now and it now tells me the tax and the sum correctly. I only have two more questions to make it perfect if someone would be kind enough to help me When it says the tax it says it like this The tax is 102413.85%..how do I get that percent sign out of there and put a dollar sign in the front?

The second questions about my only problem. When it asks for the input the first time it always says the tax is 0 for no matter what I put in but then works correctly after the first input. For example

Please input the income, type -1 to end
3453
The tax is 0.00% Please input the income, type -1 to end
234234
The tax is 65747.22% Please input the income, type -1 to end
2345
The tax is 234.50% Please input the income, type -1 to end

Why does it do that and how can I fix this? If there are any more problems with my program PLEASE let me know I have worked all day on it, it looks fine too me but I do not know anything and there could easily be another problem . Thank you!!

Code :

```public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = 0.1; final double MEDTAXRATE = 0.15; final double HIGHTAXRATE = 0.33; double sumOfTax=0; System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); double tax=0;     while(income>=0) { sumOfTax = sumOfTax + tax; tax = tax + 0; System.out.printf("The tax is %.2f%% ", tax); System.out.println("Please input the income, type -1 to end"); income = keyboard.nextDouble();       if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000)*HIGHTAXRATE; else if(income<=60000&&income>15000) tax=1500+(income-15000)*MEDTAXRATE; }       System.out.printf("The sumOfTax is %.2f%%", sumOfTax);         }   }```
• March 11th, 2010, 12:28 AM
helloworld922
Re: Calculate federal taxes program! Help!
To display out a dollar sign, simply tell it to print out the dollar sign.

Code :

`System.out.println("This is a dollar amount: \$%0.2f",1.23); // will print out \$1.23`

Again, you're printing out information before you've calculated it. Simply shuffle the code you have for computing tax and where you print out the information about tax.

Code :

```if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000)*HIGHTAXRATE; else if(income<=60000&&income>15000) tax=1500+(income-15000)*MEDTAXRATE; } System.out.printf("The tax is \$%.2f ", tax); System.out.printf("The sumOfTax is \$.2f",sumOfTax); System.out.println("Please input the income, type -1 to end"); income = keyboard.nextDouble();```
• March 11th, 2010, 12:43 AM
ocmjt
Re: Calculate federal taxes program! Help!
Thank you again helloworld. I have the dollar sign now but is there any way I can get it not to use the percent in the answer? And for the second part I did what you said and with that the tax answer doesnt even come up. Please help ;)

Code :

``` public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = 0.1; final double MEDTAXRATE = 0.15; final double HIGHTAXRATE = 0.33; double sumOfTax=0; System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); double tax=0;     while(income>=0) { sumOfTax = sumOfTax + tax;       if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000)*HIGHTAXRATE; else if(income<=60000&&income>15000) tax=1500+(income-15000)*MEDTAXRATE; }     System.out.printf("The tax is \$%.2f%% ", tax); System.out.printf("The total tax is \$%.2f%%", sumOfTax); System.out.println("Please input the income, type -1 to end"); income = keyboard.nextDouble();       }   }```
• March 11th, 2010, 12:56 AM
helloworld922
Re: Calculate federal taxes program! Help!
oops, my bad. I forgot to shuffle the sumOfTax calculation around. that line should go underneath the if/else statements (after you've calculated the new tax dollar cost).

You still have the "%%" in your printf statements. If you notice in my above post, I removed them. Also, it would be a good idea to print out some newline characters to make your output clear-er.

Code :

```System.out.printf("The tax is \$%.2f\n ", tax); System.out.printf("The sumOfTax is \$.2f\n",sumOfTax);```
• March 11th, 2010, 01:12 AM
ocmjt
Re: Calculate federal taxes program! Help!
I dont think I put it in the right spot? Still no answer comes up?

Code :

```public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = 0.1; final double MEDTAXRATE = 0.15; final double HIGHTAXRATE = 0.33; double sumOfTax=0; System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); double tax=0;     while(income>=0) {         if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000)*HIGHTAXRATE; else if(income<=60000&&income>15000) tax=1500+(income-15000)*MEDTAXRATE;   }   sumOfTax = sumOfTax + tax; System.out.printf("The tax is \$%.2f ", tax); System.out.printf("The total tax is \$%.2f ", sumOfTax); System.out.println("Please input the income, type -1 to end"); income = keyboard.nextDouble();       }   }```
• March 11th, 2010, 01:18 AM
ocmjt
Re: Calculate federal taxes program! Help!
I think I got it..thanks a lot for your help! Can you explain what you mean with newline characters?
and does this look right?
Code :

```import java.util.Scanner; public class IncomeTax {   /** * This program calculates federal taxes on a list containing various incomes * Author: Marco Tomasello * Date: March 8, 2010 */ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); final double LOWTAXRATE = 0.1; final double MEDTAXRATE = 0.15; final double HIGHTAXRATE = 0.33; double sumOfTax=0; System.out.println("Please input the income, type -1 to end"); double income = keyboard.nextDouble(); double tax=0;     while(income>=0) {         if(income<=15000) tax=income*LOWTAXRATE; else if(income>60000) tax=8250+(income-60000)*HIGHTAXRATE; else if(income<=60000&&income>15000) tax=1500+(income-15000)*MEDTAXRATE;       sumOfTax = sumOfTax + tax; System.out.printf("The tax is \$%.2f ", tax); System.out.println("Please input the income, type -1 to end"); income = keyboard.nextDouble(); } System.out.printf("The total tax is \$%.2f ", sumOfTax);     }   }```
• March 11th, 2010, 10:25 AM
helloworld922
Re: Calculate federal taxes program! Help!
Yeah, I guess that looks like what you want. Newline characters are what put lines of text on different lines. Ex:

Code :

```System.out.println("This has no new lines. These two sentences are on the same line."); System.out.println("The new line character is \\n.\n These two sentences are on different lines.");```