The below code is an action listener for a project. Basically, I have 4 radio buttons and when I click one, I want it to change a variable on screen. When I run the code, it is simply adding together all the values. Are there any other ways to do this?
class Calc implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double base = 0.00;
double options;
double total;
if (Button25.isSelected());
{
base = base + 999.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button32.isSelected());
{
base = base + 1049.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button35.isSelected());
{
base = base + 1099.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
if (Button42.isSelected());
{
base = base + 1155.99;
String base2 = Double.toString(base);
lblBaseAns.setText(base2);
}
}
}
The problem is that, for each
if()statement, such as if (Button32.isSelected());, you have a;symbol at the end. This shouldn’t be there. Here is the corrected code…As an alternative, why don’t you get the button that was clicked from the
ActionEvent, and then use the button in your if-else branch…