Calculator does not work, does not add weekly, monthly or yearly correctly

Hi I am making a calculator where the user inputs his/her income (wages, loans, other) and spending (rent, food, other). I am an if else statement to add up the input fields as the user can input the numbers according to weekly, monthly or yearly. It allos me to run the code but when I input 10 pounds in each of the 3 fields the result is 30 but it should have calculated as 10 * 4.3333333 for each of the three fields.

The if else statement is below;

double totalIncome = num1 + num2 + num3;

double totalSpending = num4 + num5 + num6;

double result = 0;

if (op1.equals("Weekly"))

result = num1 * 4.3333333;

else if (op1.equals("Monthly"))

result = num1;

else if (op1.equals("Yearly"))

result = num1 / 12;

else if

(op2.equals("Weekly"))

result = num2 * 4.3333333;

else if (op2.equals("Monthly"))

result = num2;

else if (op2.equals("Yearly"))

result = num2 / 12;

else if

(op3.equals("Weekly"))

result = num3 * 4.3333333;

else if (op3.equals("Monthly"))

result = num3;

else if (op3.equals("Yearly"))

result = num3 / 12;

TotalIncomeField.setText( "" + totalIncome);

THE WHOLE STATEMENT LOOKS LIKE;

try {

double num1 = Double.parseDouble(LoansField.getText());

double num2 = Double.parseDouble(WagesField.getText());

double num3 = Double.parseDouble(OtherField.getText());

double num4 = Double.parseDouble(RentField.getText());

double num5 = Double.parseDouble(FoodField.getText());

double num6 = Double.parseDouble(OtherField2.getText());

String op1 = (String)LoanComboBox.getSelectedItem();

String op2 = (String)WagesComboBox.getSelectedItem();

String op3 = (String)OtherComboBox.getSelectedItem();

String op4 = (String)RentComboBox.getSelectedItem();

String op5 = (String)FoodComboBox.getSelectedItem();

String op6 = (String)Other2ComboBox.getSelectedItem();

double totalIncome = num1 + num2 + num3;

double totalSpending = num4 + num5 + num6;

double result = 0;

if (op1.equals("Weekly"))

result = num1 * 4.3333333;

else if (op1.equals("Monthly"))

result = num1;

else if (op1.equals("Yearly"))

result = num1 / 12;

else if

(op2.equals("Weekly"))

result = num2 * 4.3333333;

else if (op2.equals("Monthly"))

result = num2;

else if (op2.equals("Yearly"))

result = num2 / 12;

else if

(op3.equals("Weekly"))

result = num3 * 4.3333333;

else if (op3.equals("Monthly"))

result = num3;

else if (op3.equals("Yearly"))

result = num3 / 12;

TotalIncomeField.setText( "" + totalIncome);

if

(op4.equals("Weekly"))

result = num4 * 4.3333333;

else if (op4.equals("Monthly"))

result = num4;

else if (op4.equals("Yearly"))

result = num4 / 12;

else if

(op5.equals("Weekly"))

result = num5 * 4.3333333;

else if (op5.equals("Monthly"))

result = num5;

else if (op5.equals("Yearly"))

result = num5 / 12;

else if

(op6.equals("Weekly"))

result = num6 * 4.3333333;

else if (op6.equals("Monthly"))

result = num6;

else if (op6.equals("Yearly"))

result = num6 / 12;

TotalSpendingField.setText( "" + totalSpending);

} catch (NumberFormatException ex) {

}

}

[3463 byte] By [ltanseya] at [2007-10-3 8:25:59]
# 1

First of all, you are calculating 'result', but you don't use it anywhere. Instead you use 'totalIncome' and 'totalSpending', which are calculated without the daily/yearly corrections.

Also, there are some 'else' keywords that should not be there. If (op1.equals("Weekly")) will be true, no other part of the if block for num1, num2, num3 will execute. It should look like this:

if (op1.equals("Weekly")) {

num1 *= 4.3333333;

} else if (op1.equals("Yearly")) {

num1 /= 12;

}

if (op2.equals("Weekly")) {

num2 *= 4.3333333;

} else if (op2.equals("Yearly")) {

num2 /= 12;

}

if (op3.equals("Weekly")) {

num3 *= 4.3333333;

} else if (op3.equals("Yearly")) {

num3 /= 12;

}

double totalIncome = num1 + num2 + num3;

TotalIncomeField.setText( "" + totalIncome);

Similarly for the spending.

Cinnama at 2007-7-15 3:32:19 > top of Java-index,Java Essentials,New To Java...
# 2

Please repost the code using [code][/code] tags and indention. It will make your code readable.

It looks like this is the only code that will throw a NumberFormatException:

double num1 = Double.parseDouble(LoansField.getText());

double num2 = Double.parseDouble(WagesField.getText());

double num3 = Double.parseDouble(OtherField.getText());

double num4 = Double.parseDouble(RentField.getText());

double num5 = Double.parseDouble(FoodField.getText());

double num6 = Double.parseDouble(OtherField2.getText());

So that's the only part that should be in the try catch block.

hunter9000a at 2007-7-15 3:32:19 > top of Java-index,Java Essentials,New To Java...