Hi Melissa,
So if I get you correctly Everything seems to work fine except when there is nothing in the TextField and you click between the radio buttons.
A simple way of solving this would be if there is no data in the TextField make it so none of the calculations are carried out.
One way of doing that is simply add Code:
&& enterTempField.getText().length() != 0
To each of the IF statments. That way it checks to see if the length of the TextField is greater than 0 otherwise it wont carry out any of the calcuations.
So try this for example
Code:
class RButHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
//formats the result temperature to two decimal places
DecimalFormat num = new DecimalFormat(",###.##");
//data declaration section
String instring;
double temp, outvalue;
if (e.getSource() == rdbutFtoC && enterTempField.getText().length() != 0) //if FtoC button selected
{
instring = enterTempField.getText(); //reads the input into the textfield
temp = Double.parseDouble(instring); //converts the text to a double
outvalue = 5.0/9.0 * (temp - 32); //formula for F to C conversion
//make answer appear in answer text field
answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Celsius.");
}
else if (e.getSource() == rdbutCtoF && enterTempField.getText().length() != 0)
{
instring = enterTempField.getText(); //reads the input into the textfield
temp = Double.parseDouble(instring); //converts the text to a double
outvalue = (9.0/5.0 * temp) + 32; //formula for C to F conversion
//make answer appear in answer text field
answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Farenheit.");
}
else if (e.getSource() == rdbutFtoK && enterTempField.getText().length() != 0)
{
instring = enterTempField.getText(); //reads the input into the textfield
temp = Double.parseDouble(instring); //converts the text to a double
outvalue = 5.0/9.0 * (temp - 32) + 273; //formula for F to K conversion
//make answer appear in answer text field
answerField.setText(temp + " degrees Farenheit is " + num.format(outvalue) + " degrees Kelvin.");
}
else if (e.getSource() == rdbutCtoK && enterTempField.getText().length() != 0)
{
instring = enterTempField.getText(); //reads the input into the textfield
temp = Double.parseDouble(instring); //converts the text to a double
outvalue = temp + 273; //formula for C to K conversion
//make answer appear in answer text field
answerField.setText(temp + " degrees Celsius is " + num.format(outvalue) + " degrees Kelvin.");
}
}
} There are many other possible ways of doing this like enclosing all of the event handling data in one big IF or instead using something like enterTempField.getText().equalsIgnoreCase("") or enterTempField.getText().matches() and use regular expression to see if the TextField is empty.
Either Way I hope that solves your problem