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

Thread: Calculate federal taxes program! Help!

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

    	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);
     
     
     
     
    }
     
    }
    Last edited by helloworld922; March 10th, 2010 at 11:01 AM.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    		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:
    tax = keyboard.nextDouble();

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

    	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);
     
     
     
     
    }
     
    }
    Last edited by helloworld922; March 10th, 2010 at 05:39 PM.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.
    System.out.println("Please input the income, type -1 to end");
    double income = keyboard.nextDouble();

    and please surround your code with [code] tags

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!!!!

    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);
     
     
     
     
    }
     
    }

  7. #7
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!!

    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);
     
     
     
     
    }
     
    }

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculate federal taxes program! Help!

    To display out a dollar sign, simply tell it to print out the dollar sign.

    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.

    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();

  10. #10
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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

    	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();
     
     
     
    }
     
    }

  11. #11
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

    System.out.printf("The tax is $%.2f\n ", tax);	
    			System.out.printf("The sumOfTax is $.2f\n",sumOfTax);

  12. The Following User Says Thank You to helloworld922 For This Useful Post:

    ocmjt (March 11th, 2010)

  13. #12
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Calculate federal taxes program! Help!

    I dont think I put it in the right spot? Still no answer comes up?

    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();
     
     
     
    }
     
    }

  14. #13
    Junior Member
    Join Date
    Mar 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?
    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);	
     
     
    }
     
    }

  15. #14
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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.");

  16. The Following User Says Thank You to helloworld922 For This Useful Post:

    ocmjt (March 11th, 2010)

Similar Threads

  1. Check difference between no. of stops, calculate cost
    By JavaStudent23 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 17th, 2009, 03:29 AM
  2. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2009, 12:41 AM
  3. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 04:31 PM