Simple java conversion driving me crazy!!
Hello everyone. I have to code a program to convert Farenheit to Celcius with a simple formula. The object is to collect input from the user via a JTextField and display the output in a JLabel... My code is below... any additional input would be greatly appreciated! I did not supply the test class but I can if needed.
Code:
package WindowBuilderTest;
//import java.awt.BorderLayout;
//import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Demo extends JFrame
{
private JPanel contentPane;
private JTextField textField;
private double farenheit;
private double celciusConv;
private JLabel celciusLabel;
public Demo()
{
super("Convert Farenheit to Celcius");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
celciusLabel = new JLabel();
celciusLabel.setBounds(78, 55, 180, 14);
contentPane.add(celciusLabel);
textField = new JTextField();
textField.setText("Enter Farenheit Temp");
textField.setBounds(127, 148, 150, 20);
contentPane.add(textField);
textField.setColumns(10);
textField.selectAll();
JButton btnConvert = new JButton("Convert");
btnConvert.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
calcTemp();
celciusLabel.setText(String.format("Degrees in Celcius = %.4f", celciusConv));
}
});
btnConvert.setBounds(312, 147, 89, 23);
contentPane.add(btnConvert);
} //
public void calcTemp()
{
celciusConv = ( farenheit - 32) * 5 / 9;
}
{
}
}
Re: Simple java conversion driving me crazy!!
Now we know what you're code is supposed to do; did you have a question?
kind regards,
Jos
Re: Simple java conversion driving me crazy!!
The conversion is not working properly. If a value of 100 is entered, it returns -17 when the output should be 37.777, based on my output format. However, after clicking the ok button it doesn't work.
Re: Simple java conversion driving me crazy!!
Where in the program are you setting the value for your farenheit variable?
I believe that is your biggest issue.
Re: Simple java conversion driving me crazy!!
As I stated previously, there is a test class to display the jframe. Here is the code for that class.
package WindowBuilderTest;
import java.awt.EventQueue;
public class JFrame
{
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try {
Demo frame = new Demo();
frame.setSize(500, 300);
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
Re: Simple java conversion driving me crazy!!
Quote:
Originally Posted by
New2J@v@
The conversion is not working properly. If a value of 100 is entered, it returns -17 when the output should be 37.777, based on my output format. However, after clicking the ok button it doesn't work.
In method calcTemp(), print the value of variable fahrenheit and see if it contains the value you expected. You can use a simply System.out.println( ... ) so the value will be printed on your console.
kind regards,
Jos
Re: Simple java conversion driving me crazy!!
Again, take a look at your Demo class. You declared your double variable farenheit, but where in the program are you setting its value?
Re: Simple java conversion driving me crazy!!
That was the key! As usual, I instantly thought something larger was wrong, but after you point that out I realized I did not have a get statement to pull in the input value. After doing that she worked like a champ.
Thanks again!