-2

I am writing a program where I need to input the following: customer Id, their income, federal withholding,state withholding and deductions. However, when I run my program it asks for my customerID and that is it... I am not sure what is incorrect here.

 // Get first Customer ID
{ while(customerID != -1)
{
  System.out.print("Enter Customer ID: ");
  customerID = input.nextInt();
  // Get income and withholding information

  System.out.print("Enter Income: ");
       income = input.nextDouble();

  System.out.print("Enter Federal Taxes Withheld: ");
  federalwh = input.nextDouble();

  System.out.print("Enter State Taxes Withheld: ");
  statewh = input.nextDouble();

  System.out.print("Enter Deductions: ");
  deduction = input.nextDouble();
}

  // Get next Customer ID
 System.out.println("Enter Customer ID: ");
  customerID = input.nextInt();
guinea2
  • 165
  • 1
  • 1
  • 7
  • fix the formatting and you will probably find what is wrong. – clcto Sep 24 '14 at 16:39
  • just a hint (unrelated to the problem): don't use floating point variables (i.e. double) for currency calculations. see http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Thomas Stets Sep 24 '14 at 16:43
  • Thank you, I know my formatting is off but I am new and trying. My professor wants us to use double. But than you for all your advice, I appreciate any feedback given. – guinea2 Sep 24 '14 at 17:27

2 Answers2

0

Try to remove semicolon here

if(taxableIncome > 20000 && taxableIncome <= 40000);

It should be

if(taxableIncome > 20000 && taxableIncome <= 40000)
B0WSER
  • 104
  • 4
  • Thank you any other tips for finishing my code? I am new to this and am stuck I know I will need a loop but do not know where, and the rest of the program any pointers? – guinea2 Sep 24 '14 at 16:50
0

Updated Answer for Updated Question

For flow purposes, you should query first outside the loop, and then query again at the end of the loop in preparation for the next iteration. You had the order slightly off, so weird things would have happened, but most of the code was right.

// Get the first Customer ID
System.out.println("Enter Customer ID: ");
customerID = input.nextInt();

while(customerID != -1)
{
    // Get income and withholding information
    System.out.print("Enter Income: ");
    income = input.nextDouble();

    System.out.print("Enter Federal Taxes Withheld: ");
    federalwh = input.nextDouble();

    System.out.print("Enter State Taxes Withheld: ");
    statewh = input.nextDouble();

    System.out.print("Enter Deductions: ");
    deduction = input.nextDouble();

    //!IMPORTANT SET all values for bracket to zero here!
    bracket10to20 = 0.0;
    //you can fill in all the rest

    //put all the calculations here from original answer

    //Get next customer id
    System.out.print("Enter Customer ID: ");
    customerID = input.nextInt();
    // if it's -1, we won't go through while again
}

Original Answer for Original Question

Here's the problem line:

if(taxableIncome > 20000 && taxableIncome <= 40000);

The semicolon breaks it. Remove that. However, I would suggest you change your approach to else ifs. You've nested a lot of if/elses together, when you can actually chain them like this:

    taxableIncome = income - deduction;

    if (taxableIncome <= 10000) {
        federalTax = 0.0;
    } else if (taxableIncome > 10000 && taxableIncome <= 20000) {
        bracket10to20 = (taxableIncome - 10000);
    } else if (taxableIncome > 20000 && taxableIncome <= 40000) {
        bracket20to40 = taxableIncome - 20000;
        bracket10to20 = 10000;
    } else if (taxableIncome > 40000) {
        bracket40plus = taxableIncome - 30000;
        bracket10to20 = 10000;
        bracket20to40 = 20000;
    }

    federalTax = (bracket10to20 * 0.15) + (bracket20to40 * 0.2)
            + (bracket40plus * 0.3);

This is significantly easier to read and you don't have to track all the nesting. As always, formatting reduces the chance of errors in code and makes it easier for others to help.

Compass
  • 5,867
  • 4
  • 30
  • 42
  • Thank you I know my formatting is a mess, I am trying so hard but am new to this. My professor thinks he is helpful but he makes it more confusing. Any help on the rest of the project? I am not looking for answers just guidance. – guinea2 Sep 24 '14 at 16:51
  • @guinea2 There should be an option in your Java IDE to format or auto-format. Using that will make it easier to follow your coding path. For the repeat, see [this](http://stackoverflow.com/questions/12999899/getting-user-input-with-scanner). It should help you in the right direction, if you package most of your code into the `while` loop. – Compass Sep 24 '14 at 16:55
  • I found the format button, it is very helpful. I looked at the link but it was kind of confusing I started out with while (customerId != -1) but it is still only asking me to enter the customer ID nothing else. I really appreciate you helping me. I am trying to break it down into individual sections. – guinea2 Sep 24 '14 at 17:44
  • @guinea2 I will post a fragment with the updated part. – Compass Sep 24 '14 at 17:47
  • Thank you so much, my program is far from finishes but I have a lot more completed now than before. Again thanks. – guinea2 Sep 24 '14 at 18:39
  • I am almost finished with my program, but now when I print my output at the end instead of the customer output being what I enter(101) it prints out -1 from my loop. How would I get it to output the customer ID I am entering? – guinea2 Sep 25 '14 at 06:04