Results 1 to 2 of 2

Thread: arrays

  1. #1
    durdanto is offline Member
    Join Date
    Feb 2011
    Posts
    20
    Rep Power
    0

    Default arrays

    Hi, i am trying to create program for Tax

    but i am having problem creating the tax formula using arrays
    Output should be something like the following:

    Tax filing status can be as follows:
    (0-single, 1-married, 2-widowed, 3-living common-law, 4-to quit this program)
    Enter the filing status: 0
    Enter the taxable income in $ 400000
    Tax is $117683.5


    problem is that it works only if income is 40000$
    it does work properly if input is anything else
    plz help
    Java Code:
    import java.util.Scanner;
    public class ComputeTax {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) 
    	{
    		//Scanner
    		Scanner myKeyboard = new Scanner(System.in);
    		
    		//Welcome message:
    		System.out.println("Welcome to Revenue Agency Taxation");
    		System.out.println("\nTax filing status can be as follows:" +
    				"\n(0-single, 1-married, 2-widowed, 2-living common-law, 4-to quit this program)");
    		System.out.print("\nEnter the filing status: ");
    		
    		//program starting algorithm:
    		
    		double [] rates= {0.10, 0.15, 0.25, 0.28, 0.33, 0.35}; 
    		double[][] brackets = {
    							{8350, 33950, 82250, 171550, 372950},   //single
    							{16700, 20885, 67900, 137050, 372950},  // Married
    							{8350, 33950, 68525, 104425, 186475},   // Widowed
    							{11950, 45500, 117450, 190200, 372950}  // Living common-law
    							};
    		
    		
    		
    		int i = myKeyboard.nextInt();
    		while (i<4 & i>=0)
    		{
    			
    			System.out.print("\nEnter the taxable income in $ ");
    			double income = myKeyboard.nextDouble();
    			
    			
    			
    			//tax calculation:
    			double tax = 0;
    			
    				{
    					if(i==0) //single
    					{
    					//	for (i=0; income > brackets[0][4]; i++ )
    						 tax = brackets[ 0][ 0] * rates[ 0] +
    						( brackets[ 0][ 1] - brackets[ 0][ 0]) * rates[ 1] +
    						( brackets[ 0][ 2] - brackets[ 0][ 1]) * rates[ 2] +
    						( brackets[ 0][ 3] - brackets[ 0][ 2]) * rates[ 3] +
    						( brackets[ 0][ 4] - brackets[ 0][ 3]) * rates[ 4] +
    						( income - brackets[ 0][ 4]) * rates[ 5];
    					}	 
    					if(i==1) // Married
    						 tax = brackets[ 1][ 0] * rates[ 0] +
    						( brackets[ 1][ 1] - brackets[ 1][ 0]) * rates[ 1] +
    						( brackets[ 1][ 2] - brackets[ 1][ 1]) * rates[ 2] +
    						( brackets[ 1][ 3] - brackets[ 1][ 2]) * rates[ 3] +
    						( brackets[ 1][ 4] - brackets[ 1][ 3]) * rates[ 4] +
    						( income - brackets[ 1][ 4]) * rates[ 5];
    					if(i==2) // Widowed
    						 tax = brackets[ 0][ 0] * rates[ 0] +
    						( brackets[ 2][ 1] - brackets[ 2][ 0]) * rates[ 1] +
    						( brackets[ 2][ 2] - brackets[ 2][ 1]) * rates[ 2] +
    						( brackets[ 2][ 3] - brackets[ 2][ 2]) * rates[ 3] +
    						( brackets[ 2][ 4] - brackets[ 2][ 3]) * rates[ 4] +
    						( income - brackets[ 2][ 4]) * rates[ 5];
    					if(i==3) // Living common-law
    						 tax = brackets[ 3][ 0] * rates[ 0] +
    						( brackets[ 3][ 1] - brackets[ 3][ 0]) * rates[ 1] +
    						( brackets[ 3][ 2] - brackets[ 3][ 1]) * rates[ 2] +
    						( brackets[ 3][ 3] - brackets[ 3][ 2]) * rates[ 3] +
    						( brackets[ 3][ 4] - brackets[ 3][ 3]) * rates[ 4] +
    						( income - brackets[ 3][ 4]) * rates[ 5];
    					
    				}
    				
    			
    				System.out.println("\nTax is $" + tax );
    		}
    		System.out.println("\nGood Bye" );
    	}
    
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    It would be better if you use Objects rather than Arrays. You can do this better with an Enum set. Please see how I solved this tax calculation solution for more details:

    Problem working out Assignment

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 11:32 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  3. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 01:33 PM
  4. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 06:12 AM
  5. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 03:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •