I am new to programming and just started last week, so all the java mumbojumbo is quite confusing to me. I was able to create a option pane for my BMI program that asks which unit system(metric/imperial) with radiobuttons and this determines which calculation to perform when finding the BMI. this all works fine except the first optionpane doesn't close when an option is selected, how do i make it close when an option is seleceted. I want the jpane with radiobuttons to close in the do statement.
package javaapplication21;
import java.text.DecimalFormat;
import javax.swing.*;
import java.*;
public class JavaApplication21 {
public static void main(String[] args) {
JPanel jPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
JRadioButton metricButton = new JRadioButton("Metric");
metricButton.setActionCommand("Metric");
JRadioButton imperialButton = new JRadioButton("Imperial");
imperialButton.setActionCommand("Imperial");
group.add(metricButton);
group.add(imperialButton);
jPanel.add(metricButton);
jPanel.add(imperialButton);
JOptionPane.showOptionDialog(null, "Please select prefered units", "BMI Calculator", JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE, null, new Object[] { metricButton, imperialButton}, null);
DecimalFormat oneDigit = new DecimalFormat("#,##0.0");
double bodyMassIndex, weight, height;
String unitsWeight = "-1", unitsHeight = "-1";
do{
if (metricButton.isSelected()){
unitsWeight = " in kg.";
unitsHeight = " in meters";
}
else if (imperialButton.isSelected()){
unitsWeight = " in lbs";
unitsHeight = " in inches";
}
}
while ("-1".equals(unitsWeight));
String weightInput = JOptionPane.showInputDialog("Please enter your weight" + unitsWeight);
String heightInput = JOptionPane.showInputDialog("Please enter your height" + unitsHeight);
if (metricButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = weight / (height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("You are at high risk of many health concerns");
}
else if (imperialButton.isSelected()){
height = Double.parseDouble(heightInput);
weight = Double.parseDouble(weightInput);
bodyMassIndex = (weight * 703) / (height * height);
System.out.println("Your Body Mass Index(BMI) is " + oneDigit.format(bodyMassIndex) + "kg/m^2");
if (bodyMassIndex < 15)
System.out.println("You are starving");
else if (bodyMassIndex < 18.5)
System.out.println("You are underweight");
else if (bodyMassIndex < 25)
System.out.println("You are healthy");
else if (bodyMassIndex < 30)
System.out.println("You are obese");
else if (bodyMassIndex < 40)
System.out.println("You are morbidly obese");
else
System.out.println("No more big macs!");
}
}
}