// Convert Celsius to Fahrenheit and vice versa.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ConvertTemp extends JFrame {
    // instance variables 
    JLabel labelFahr;
    JLabel labelCels;
    JTextField textFahr;
    JTextField textCels;
    JButton conFahToCel;
    JButton conCelToFah;

    public ConvertTemp () {

        super ("Temperature");
        setLayout (new FlowLayout ());

        labelFahr = new JLabel ("Fahrenheit: ", SwingConstants.LEFT);
        labelFahr.setToolTipText("This is a temerature scale");
        add (labelFahr);
        textFahr = new JTextField (10);
        add (textFahr);

        labelCels = new JLabel ("Celsius:       ", SwingConstants.LEFT);
        labelCels.setToolTipText("This is a scale and unit of measurement for temperature");
        add (labelCels);
        textCels = new JTextField (10);
        add (textCels);

        conFahToCel = new JButton ("Convert Fahrenheit to Celsius");
        add (conFahToCel);
        conCelToFah = new JButton ("Convert Celsius to Fahrenheit");
        add (conCelToFah);

        JPanel panel = new JPanel(new GridLayout(2, 2, 12, 6));
        panel.add(labelFahr);
        panel.add(labelCels);
        panel.add(textFahr);
        panel.add(textCels);
        add(panel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(conFahToCel);
        buttonPanel.add(conCelToFah);   
        add(buttonPanel, BorderLayout.SOUTH);

        conFahToCel.addActionListener(new FahrListener ());
        conCelToFah.addActionListener(new CelsListener ());

    } // end constructor 

    private class FahrListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == conFahToCel){
                int conFahToCel = (int) ((5.0/9.0 * (((Double.parseDouble(textFahr.getText())) -32))));
                textCels.setText(conFahToCel + " °C");
                textFahr.requestFocus();
                textFahr.selectAll();
            } // end if statement

        } // end actionPerformed
    } // end FahrListener

    private class CelsListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == conCelToFah){
                int conCelToFah = (int) (( 9.0/5.0 * (((Double.parseDouble(textCels.getText())))))) + 32))));
                textFahr.setText(conCelToFah + " °F");
                textCels.requestFocus();
                textCels.selectAll();
            } // end if statement

        } // end actionPerformed
    } // end CelsListener

} // end class ConvertTemp

Test class

import javax.swing.JFrame;
public class ConvertTempTest {
    public static void main(String[] args) {

        ConvertTemp app = new ConvertTemp ();
        app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        app.setSize (500, 150); // set the frame size
        app.setVisible (true); // display frame
    } // end main method
} // end class ConvertTempTest

Can someone please help me with what is wrong? I have no clue.

Recommended Answers

All 4 Replies

Is it wrong? In what way? Do you have an error message? Does it goive the wrong results? You must tell us everything you know if you want help.

The problem is that it will not save. The error message is attatched. Thank you.

The message is pretty self-explanatory. It probably refers to the degree symbols on lines 63 and 75 (there may be others as well) because there is no degree symbol in your default US-ASCII character set.
The simplest/safest fix is to remove those symbols - they are just part of the display and have no effect on the program logic.

Thank you!

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.