Hey everyone. I have a problem. I can't find why my code won't convert Fehrenheit to Celsius. :sad:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CTemp extends JFrame
	implements ActionListener {

	private JButton button;

	public static void main(String[] args) {
		CTemp frame = new CTemp();
		frame.setSize(400, 300);
		frame.createGUI();
		frame.setVisible(true);

}

	private void createGUI() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container window = getContentPane();
		window.setLayout(new FlowLayout() );

		button = new JButton("Fahrenheit converted to Celsius");
		window.add(button);
		button.addActionListener(this);
}
	public void actionPerformed(ActionEvent event) {

		int fahrenheit, celsius; 
		String fahrenheitString;
		

		fahrenheitString = JOptionPane.showInputDialog("Tempature in Fahrenheit: ");
		fahrenheit = Integer.parseInt(fahrenheitString);
		celsius = (5 / 9) * (fahrenheit - 32);
		JOptionPane.showMessageDialog(null, fahrenheit + " Degrees Fahrenheit\n" + "converted to\n" + celsius + " Degrees Celsius");


	}
}

Recommended Answers

All 8 Replies

Use double for your fahrenheit and celsius and change
celsius = (5 / 9) * (fahrenheit - 32);
to
celsius = (((fahrenheit - 32) / 9) *5)

Hey everyone. I have a problem. I can't find why my code won't convert Fehrenheit to Celsius. :sad:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CTemp extends JFrame
    implements ActionListener {

    private JButton button;

    public static void main(String[] args) {
        CTemp frame = new CTemp();
        frame.setSize(400, 300);
        frame.createGUI();
        frame.setVisible(true);

}

    private void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout() );

        button = new JButton("Fahrenheit converted to Celsius");
        window.add(button);
        button.addActionListener(this);
}
    public void actionPerformed(ActionEvent event) {

        int fahrenheit, celsius; 
        String fahrenheitString;
        

        fahrenheitString = JOptionPane.showInputDialog("Tempature in Fahrenheit: ");
        fahrenheit = Integer.parseInt(fahrenheitString);
        celsius = (5 / 9) * (fahrenheit - 32);
        JOptionPane.showMessageDialog(null, fahrenheit + " Degrees Fahrenheit\n" + "converted to\n" + celsius + " Degrees Celsius");


    }
}

Maybe I'm missing this, but where exactly are you getting the input from the user?

Use double for your fahrenheit and celsius and change
celsius = (5 / 9) * (fahrenheit - 32);
to
celsius = (((fahrenheit - 32) / 9) *5)

You do not need to change f an c to double, you just need to make sure that you do double math (if all you want is the nearest whole c grad).

The formula used initially is okay, except for the fact that he is not doing "double math", so the quickest fix, that works, is as follows:

change

celsius = (5 / 9) * (fahrenheit - 32);

to

celsius = ((5.0 / 9) * (fahrenheit - 32));

@MacGyver Orca
He gets his input here:

fahrenheitString = JOptionPane.showInputDialog("Tempature in Fahrenheit: ");

Thanks guys. Yeh that formula was wrecking my brain. I went ahead with Badapples formula. Masijade. For some reason yours did not compile ok.

The message you got was only a compiler warning. The class itself still compiled normally. If you want to get rid the warning change

celsius = ((5.0 / 9) * (fahrenheit - 32));

to

celsius = (int) ((5.0 / 9) * (fahrenheit - 32));

and the warning goes away.

@MacGyver Orca
He gets his input here:

fahrenheitString = JOptionPane.showInputDialog("Tempature in Fahrenheit: ");

Really, I had no idea you could grab input from a JOptionPane without some sort of getText() method

What results are you getting?
Have you tried 5.0/9.0, Java will assume 5/9 are integers, but that is 0.55555 which will be just 0 as an int... it doesn't round, it truncates... or better yet, use (F-32)/1.8 you should make your variables something capable of carrying a fractional component...

Really, I had no idea you could grab input from a JOptionPane without some sort of getText() method

JOptionPane's showInputDialog and showInternalInputDialog will return a String, its showConfirmDialog, showInternalConfirmDialog, showInternalOptionDialog, and showOptionDialog return ints. With the OptionDialogs that is the index of the array of options passed.

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.