Stewart Clay Stewart Clay - 22 days ago 5
Java Question

GUI calculation issue

i have been trying for ever now trying to figure out why my code for my JFrame wont show the correct calculations! When i add all the numbers in windows Calculator they are correct but when i use my program it is different and incorrect. In my program i get the answer 520420 instead of 52420,5 which i get in windows calculator. Could someone please tell me where i am going wrong with my program and get the correct answer i would get with windows calculator?
Bellow is my calculate button which adds numbers to the total price if a check box is selected!

private void calculatorBActionPerformed(java.awt.event.ActionEvent evt) {
double price = 0;

if(shoesTK.isSelected())
{
price = price + 120;
}
if(cricketBatCK.isSelected())
{
price = price + 300;
}
if(bikeCK.isSelected())
{
price = price + 20000;
}
if(watchCK.isSelected())
{
price = price + 500000;
}
if(plasticBagCK.isSelected())
{
price = price + 0.50;
}

System.out.println(price);
String total = Double.toString(price);
totalTXT.setText(total);
}


Output of my program with the total being incorrect.
enter image description here

Output of windows calcultor and the result i want.

enter image description here

Answer
if(bikeCK.isSelected())
{
    price = price + 20000;
}
if(watchCK.isSelected())
{
    price = price + 500000;
}

You've got a trailing zero too much in both of these cases. It should be 2000 and 50000 respectively.

Also, in your text you say that you get 520420 but your screen shot actually shows 520420.5 so it looks like the plastic bag is handled correctly.