0

I am very new to java and my if statements don't seem to be assigning values to variables. I would really appreciate some help. The output is always 1000

DecimalFormat df = new DecimalFormat("#.00");

    // Defines amount and bonus
    double amount; 
    double bonus;

    bonus = 0;

    amount = Double.parseDouble(txtAmountEarnt.getText());

    if (amount <= 2000) {
        bonus = 0;
    } 
    if (amount >2499 && amount <5000) {
        bonus = 500;
    } 
    if (5000 < amount); {
        bonus = 1000;
    }

    System.out.println(bonus);
    lblBonusAwarded.setText("Bonus awarded for earning $" + (df.format(amount)) + " = $" + (df.format(bonus)));
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
A. Price
  • 23
  • 1
  • 1
  • 5

2 Answers2

3

There is a semicolon after the last if statement, which means the bonus gets set to 1000 no matter what.

Remove it and everything should be fine.

ravitimm
  • 114
  • 8
1

Your current code could be simplified to one if block (with a nested if-else), you've already initialized bonus to 0 - so you don't need to explicitly do it again.

// Defines amount and bonus
double amount = Double.parseDouble(txtAmountEarnt.getText());
double bonus = 0;
if (amount > 2499) {
    if (amount > 5000) {
        bonus = 1000;
    } else {
        bonus = 500;
    } 
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249