Reputation: 45
I'm sorry for the weird title, I didn't really know how to describe it. Basically I have a code that once the if statement is called, it changed the value of the integer, taxrate. I thought it was working, but once I try to call it into a JTextField in another part of the code, it shows up as 0. The IF statement is the following:
if (taxable >= 1 && taxable <= 9075){
taxrate = (int) (taxable * .10);
} else if (taxable >= 9076 && taxable <= 36900) {
taxrate = (int) (taxable * .15 + 908);
} else if (taxable >= 36901 && taxable <= 89350) {
taxrate = (int) (taxable * .25 + 5082);
} else if (taxable >= 89351 && taxable <= 186350){
taxrate = (int) (taxable * .28 + 18195);
} else if (taxable >= 186351 && taxable <= 405100){
taxrate = (int) (taxable * .33 + 45355);
} else if (taxable >= 405101 && taxable <= 406750) {
taxrate = (int) (taxable * .35 + 45883);
} else if (taxable >= 406751) {
taxrate = (int) (taxable * .396);
}
taxable is defined by the user, it has no defined value until the user inputs it.
Upvotes: 0
Views: 76
Reputation: 5471
First of all you need to clear up your code
if (taxable >= 1 && taxable <= 9075){
taxrate = (taxable * .10);
} else if (taxable >= 9076 && taxable <= 36900) {
taxrate = (taxable * .15 + 908);
} else if (taxable >= 36901 && taxable <= 89350) {
taxrate = (taxable * .25 + 5082);
} else if (taxable >= 89351 && taxable <= 186350){
taxrate = (taxable * .28 + 18195);
} else if (taxable >= 186351 && taxable <= 405100){
taxrate = (taxable * .33 + 45355);
} else if (taxable >= 405101 && taxable <= 406750) {
taxrate = (taxable * .35 + 45883);
} else if (taxable >= 406751) {
taxrate = (taxable * .396);
}
What is the point of having decimals if you cast them to an INT ? Integers can not have decimal places. If you want to have Decimals use Double - otherwise just dont use decimals.
Also make sure that taxable
is initialized before the if block is executed
i.e.
private double taxable = "some value";
Upvotes: 0
Reputation: 3361
Any value of taxable between 0 and 10 (excluding 10) will give you a taxrate of 0 with the code working flawlessly. If this is not your case, please make sure that your taxable value is actually assigned before this comparison is made. It is possible that the program is never actually getting to the code block inside the if statement.
Upvotes: 2