Ask coding questions

← Back to all posts
Initializing doubles in a loop giving error “variable might not have been initialized”
L0k3d20uz4 (0)

This program is supposed to get the weight of some mail from the user and calculate how much it costs. After 100g, the cost increases by $2.5/50g. However, when I try to run the program, it says "variable cost might not have been initialized". Is this because I am initializing it in an if statement or something? I know some people have had errors because they declare the variable in the if statement, but I have declared my variable outside the if statement. What am I doing wrong?

Answered by InvisibleOne (2681) [earned 5 cycles]
View Answer
InvisibleOne (2681)

I don't know java, but in some languages you have to give it a value, so maybe changing line 7 to double cost = 0; might work, idk