I need to create a Java Temperature converter. I have everthing working except when I try to convert F to C I get nothing except the value in F field is changed to 32. The conversion for C to F is working fine. Any ideas what it coudl be??

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.JOptionPane;


public class Converter extends JFrame {
//create fields for input
private JTextField jtfCelsius= new JTextField("0");
private JTextField jtfFarenheit= new JTextField("0");

private convert convert=new convert();


//create calculate button
private JButton jbtConvert=new JButton("Convert");

public Converter() {
//panel p1 holds labels n text fields
JPanel p1=new JPanel(new GridLayout(5,2));
p1.add(new JLabel("Temperature in Celsius"));
p1.add(jtfCelsius);
p1.add(new JLabel("Temperature in Farenheit"));
p1.add(jtfFarenheit);
p1.setBorder(new
      TitledBorder("Enter Temperature"));

//Panel to hold button
JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
p2.add(jbtConvert);

add(p1, BorderLayout.CENTER);
add(p2, BorderLayout.SOUTH);

jbtConvert.addActionListener(new ButtonListener());
}


   public class ButtonListener implements ActionListener {
    @Override 

      public void actionPerformed(ActionEvent e) {
            if (jtfCelsius.getText().equals(null)) {
                try {
                    double fTemp = new Double(jtfFarenheit.getText());
                    double cTemp = convert.f2c(fTemp);
                    jtfCelsius.setText("" + cTemp);
                } catch(NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(Converter.this, "Error, input must be numerical");
                }
            } else {
                try {
                    double cTemp = new Double(jtfCelsius.getText());
                    double fTemp = convert.c2f(cTemp);
                    jtfFarenheit.setText("" + fTemp);
                } catch(NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(Converter.this, "Error, input must be numerical");
                }
            }
        }






}
public static void main(String[] args) {
    Converter frame = new Converter();
    frame.pack();
    frame.setTitle("Temperature Converter");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(300,210);
    frame.setResizable(false);
  }
}




public class convert {



  public double c2f(double temp) {
        return 1.8 * temp + 32;
    }

    /**
     * converts Fahrenheit temperatures to Celsius
         * @param temp temperature in degrees Fahrenheit
         * @returns temperature in degrees Celsius
     */
    public double f2c(double temp) {
        return .556 * (temp - 32);
    }

}

Line 44 it's unlikely that getText will return null. For an empty field it will return an empty String, ie "". You can test for equals(""), or use length()==0

tried it this way

public void actionPerformed(ActionEvent e) {
            if (jtfCelsius.getText().equals("")) {
                try {
                    double fTemp = new Double(jtfFarenheit.getText());
                    double cTemp = convert.f2c(fTemp);
                    jtfCelsius.setText("" + cTemp);
                } catch(NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(Converter.this, "Error, input must be numerical");
                }

still only getting 32 after click the button and no conversion

Maybe you have one or more blanks in the field??? Try trim() before the equals(...) to eliminate any leading/trailing blanks.

Then add some temporary print statements to print all the variables as you run through that part of the code so you cen see exactly where it's going wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.