Reputation: 153
So I got this error in my program:
BankChargesWilson.java:55: error: variable checksCost might not have been initialized
System.out.println("Your total cost is " + money.format(checksCost) + ".");
^
1 error
But I don't understand why when I have the formatting object created and I initialized the checksCost variable earlier.
Heres the object + my definitions.
// Decimal Format Object
DecimalFormat money = new DecimalFormat("$##,###.##");
// Scanner Object for Input
Scanner keyboard = new Scanner (System.in);
// Variables
double baseFee = 10.00; // The base fee the bank requires for the account
double checkFee; // The fee for a check
int checksWritten; // The users inputted amount of checks written for the month
double checksCost; // The cost for the checks based on the amount written in the month
And heres my else-if statements where it's used, with the output and the spot thats causing an error.
if (checksWritten < 20)
{
checksCost = checksWritten * 0.10;
}
else if (checksWritten >= 20 && checksWritten <= 39)
{
checksCost = checksWritten * 0.08;
}
else if (checksWritten >= 40 && checksWritten <= 59)
{
checksCost = checksWritten * 0.06;
}
else if (checksWritten >= 60)
{
checksCost = checksWritten * 0.04;
}
System.out.println("You entered that you wrote " + checksWritten + " checks this month.");
System.out.println("Your total cost is " + money.format(checksCost) + ".");
I'm not sure why it's saying it's not initialized anymore.
Question though: Is the checksCost being seen in only the scope of my if-else-if loop? And if so, how come when it's defined further above?
Upvotes: 1
Views: 1736
Reputation: 1179
If checksWritten
is equal to 0
what should checksCost
equal? Initialize checksCost
to that. Along the same lines you should probably initialize checksWritten
to 0
. Also don't leave off an else
block ever because it leads to brittle code as you have just found out. So in this case just change the last else if
to else
.
Upvotes: 0
Reputation: 76
Java compiler requires that the variable must be initialized. At compile time, it doesn't check the logic of your code, so it thinks that there's a chance the variable will not be initialized if none of the "else if" match. You need to initialize checkCost before the if/else part, or put an else { double = smth; } to end the if/else clause.
Upvotes: 0
Reputation: 7494
Yes, your checksCost variable is only being see inside the block where it is initialized (in this case within the sections of the for loop).
The reason it can be seen inside the blocks of the if-else statement even though it has been declared above is because inner blocks can access code outside themselves but the reverse is not true.
Upvotes: 2
Reputation: 133567
Because the compiler doesn't analyze your code so deeply as you would imagine.
Although your if/else
chain will execute a branch for sure according to its conditions, this outcome is not realized by the compiler which complains about the fact that you are missing an else
condition.
You could solve the problem by initializing the variable or by letting last else if
be a simple else
(which wouldn't alter semantics in this specific case).
Upvotes: 3
Reputation: 121998
You need to provide a default initial value or you have to provide else
block and provide your value there.
The reason is that, What happens all of your conditions failed to execute ??
When you provide a else block, then it is sure that always there is a chance to assign the value, either in if
or in else
double checksCost;
if(){ //false
}
else if(){ //false
}
what is checksCost
value now ??
Upvotes: 2
Reputation: 33880
You need to set an initial value for the checksCost variable. There could be a case where the value never gets set. There may be a situation where none of your conditional logic would be hit.
Upvotes: 0