Rectangle 27 0

user interface Java GUI FahrenheitCelsius Converter with a Button?


celsius = Double.parseDouble(TFCelsius.getText());
 fahrenheit = Double.parseDouble(TFFahrenheit.getText());
if (!TFCelsius.getText().equals("")) {
     celsius = Double.parseDouble(TFCelsius.getText());
 } else {
     ////i don't know what the value but make it equal 0
 }

i think that your problem with The text Field ,if you didn't input numbers in the two text fields you will get "empty String" problem which cannot convert it to Double , so you need to check that you string not equal to empty

to solve it , you can make like this , For Example :

Note
Rectangle 27 0

user interface Java GUI FahrenheitCelsius Converter with a Button?


@Override
public void actionPerformed(final ActionEvent e) {
    double celsius = 0, fahrenheit = 0;
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    if (Temperature_Converter.this.TFCelsius.getText() == null || "".equals(Temperature_Converter.this.TFCelsius.getText().trim())) {
        // convert from fahrenheit to celsius
        fahrenheit = Double.parseDouble(Temperature_Converter.this.TFFahrenheit.getText());
        celsius = (fahrenheit - Temperature_Converter.offset) *   Temperature_Converter.FahrenheitTOCelsius;
        Temperature_Converter.this.TFCelsius.setText(" " + twoDigits.format(celsius));
    } else if (Temperature_Converter.this.TFFahrenheit.getText() == null || "".equals(Temperature_Converter.this.TFFahrenheit.getText().trim())) {
           // convert from celsius to fahrenheit
           celsius = Double.parseDouble(Temperature_Converter.this.TFCelsius.getText());
           fahrenheit = celsius * Temperature_Converter.CelsiusTOFarenheit + Temperature_Converter.offset;
           Temperature_Converter.this.TFFahrenheit.setText(" " + twoDigits.format(fahrenheit));
    }
}
try {
    fahrenheit = Double.parseDouble(Temperature_Converter.this.TFFahrenheit.getText());
} catch (NumberFormatException e1) {
     //alert the user       
    JOptionPane.showMessageDialog(null, "You are allowed to introduce numbers only for temperature");
    return;
}

@Mowza2k2 you are developing your CS logic by doing this kind of problems. Keep going on and you will advance a lot. By the way , watch the edit part of the post for user friendly alert message.

@Mowza2k2 you are very welcome. I'm happy you feel welcome in the community and that you find your answers here.

Edit : You can validate the user input text and alert the user if there are something else then numbers introduced as a temperature.

Here is what might help you : leave blank the text field which you want to be populated( leave blank fahrenheit if you want to convert from celsius to fahrenheit ).

This was great. It fixed my issue completely. I knew there should have been an if statement somewhere I just didn't know how to go about it. My CS logic is still not the best. Thanks!

thank you so much. Everything you posted made sense to me, though I didn't know how to go about it myself. It's nice to have somewhere to go where I can get some help (without bugging my professor).

Note
Rectangle 27 0

user interface Java GUI FahrenheitCelsius Converter with a Button?


celsius = Double.parseDouble(TFCelsius.getText());
fahrenheit = Double.parseDouble(TFFahrenheit.getText());
double celsiusNew = (fahrenheit - offset) * FahrenheitTOCelsius;
double fahrenheitNew = celsius * CelsiusTOFarenheit + offset;
TFCelsius.setText(" "+ twoDigits.format(celsiusNew));
TFFahrenheit.setText(" "+ twoDigits.format(fahrenheitNew));

Assuming that I have to put one value in the Fahrenheit field and one in the Celsius field and they should be calculated at the same time, you have to kind of "cache" your calculation values in other variables than the ones you got from the textfields.

Edit: read the other two answers as well for making your program more user-friendly and bulletproof.

Thanks a ton. Alongside the other text it all works great.

The solution for your problem is to safe your results in new variables like:

the result of your first calculation (the new Celsius value) will be used in the second calculation, leading to the very same result as the value in the Fahrenheit field originally was (because it just converts the value to Celsius and than back again)

Note
Rectangle 27 0

user interface Java GUI FahrenheitCelsius Converter with a Button?


celsius = Double.parseDouble(TFCelsius.getText());
 fahrenheit = Double.parseDouble(TFFahrenheit.getText());
if (!TFCelsius.getText().equals("")) {
     celsius = Double.parseDouble(TFCelsius.getText());
 } else {
     ////i don't know what the value but make it equal 0
 }

i think that your problem with The text Field ,if you didn't input numbers in the two text fields you will get "empty String" problem which cannot convert it to Double , so you need to check that you string not equal to empty

to solve it , you can make like this , For Example :

Note
Rectangle 27 0

user interface Java GUI FahrenheitCelsius Converter with a Button?


celsius = Double.parseDouble(TFCelsius.getText());
fahrenheit = Double.parseDouble(TFFahrenheit.getText());
double celsiusNew = (fahrenheit - offset) * FahrenheitTOCelsius;
double fahrenheitNew = celsius * CelsiusTOFarenheit + offset;
TFCelsius.setText(" "+ twoDigits.format(celsiusNew));
TFFahrenheit.setText(" "+ twoDigits.format(fahrenheitNew));

Assuming that I have to put one value in the Fahrenheit field and one in the Celsius field and they should be calculated at the same time, you have to kind of "cache" your calculation values in other variables than the ones you got from the textfields.

Edit: read the other two answers as well for making your program more user-friendly and bulletproof.

Thanks a ton. Alongside the other text it all works great.

The solution for your problem is to safe your results in new variables like:

the result of your first calculation (the new Celsius value) will be used in the second calculation, leading to the very same result as the value in the Fahrenheit field originally was (because it just converts the value to Celsius and than back again)

Note