1. Member
Join Date
Feb 2011
Posts
20
Rep Power
0

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

#### Posting Permissions

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