Newbie with Java Programming (If statements and Doubles)?
Question by Hunter2012: Newbie with Java Programming (If statements and Doubles)?
I’m a beginner at Java Programming and I’m having problems with a part of my program. Heres a piece of the code towards the end.
String Option4 = JOptionPane.showInputDialog(“For Designer Furniture with Cloth Upholstery Enter 1 n” +
“For all Above Plus Captains Chair with Leather Upholstery enter 2 n” +
“For all Above Plus Plush Carpet Cermaic Tiling, and Wood Panels Enter 3 n” +
“For all Above Plus Gold Kitchen & Bath Fixtures, Jacuzz and Sauna Enter 4 n”);
if(Integer.parseInt(Option4)== 1)
{
double opt4 = 15000;
}
else if(Integer.parseInt(Option4) == 2)
{
double opt4 = 20000;
}
else if(Integer.parseInt(Option4) == 3)
{
double opt4 = 30000;
}
else if(Integer.parseInt(Option4) == 4)
{
double opt4 = 35000;
}
else
{
String errorMessage = “Incorrect Input for Motor Home.”;
JOptionPane.showMessageDialog(null,errorMessage);
}
/** This it the calculations part of the program
*
*/
double totalHomeCost = Opt1 + Opt2 + Opt3 + opt4;
for some reason it keeps coming up with an error message saying that it cant find the symbol/variables for Opt1 Opt2 etc. All of my variables have been declared in the same manner as above with the If statements, so why wont it let me add them together?
Best answer:
Answer by jouzef19
i guess your problem in the declaration location .
because if the program does not enter to any if statement for any reason
the program will enter directly to the ELSE then to the calculation in this time it will be sub rise because the variable dose not declared in the ELSE statement
try to declared all variables outside the IF statement(Good idea) . OR in every IF statement and ELSE statement(bad idea) .
Know better? Leave your own answer in the comments!

