So the following is where I think the problem lies in my code:
public void actionPerformed(ActionEvent i) {
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//pulls the number that was entered in the input field
temp = Integer.parseInt(inputString);// makes a new variable to handle the input
//tells the output how to calculate the input based on the button that was pressed
if(i.getSource() == button) {
newtemp = (5/9) * (temp - 32);//Fahrenheit to Celsius equation
}
else if(i.getSource() == button2) {
newtemp = (5/9) * (temp - 32) + 273;//Fahrenheit to Kelvin equation
}
else if(i.getSource() == button3) {
newtemp = temp + 273;//Celsius to Kelvin equation
}
else if(i.getSource() == button4) {
newtemp = ((9/5) * temp) + 32;//Celsius to Fahrenheit equation
}
else if(i.getSource() == button5) {
newtemp = temp - 273;//Kelvin to Celsius equation
}
else {
newtemp = ((temp - 273) * (9/5)) + 32;//Kelvin to Fahrenheit equation
}
outfield.setText(" "+ three.format(newtemp));
}
}
The following is where the code fits in as a whole:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
@SuppressWarnings("serial")
public class ConvertTemp extends JFrame
implements ActionListener {
private JButton button, button2, button3, button4, button5, button6;
@SuppressWarnings("unused")
private JPanel panel;
@SuppressWarnings("unused")
private JFrame frame;
@SuppressWarnings("unused")
private JTextField Text;
@SuppressWarnings("unused")
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel = new JLabel("Temperature Value:");
private JLabel outlabel = new JLabel("Converted Temperature Value:");
DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output
public static void main(String[] args) {
ConvertTemp frame = new ConvertTemp();
frame.createFrame();
frame.createButton1();
frame.createButton2();
frame.createButton3();
frame.createButton4();
frame.createButton5();
frame.createButton6();
frame.addLabels();
}
public void createFrame() {
Container window = getContentPane();
window.setLayout(new FlowLayout() ); //using the FlowLayout manager
setSize(400, 200); //setting the size of the initial box
setVisible(true); //allows for manual size changing of the box
setTitle("Temperature Converter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout());
add(new JLabel("Temperature Conversions" + " C: Celsius" + " F: Farhrenheit"+ " K: Kelvin"));
}
public void createButton1() {
//this is to create the Fahrenheit to Celsius button
button = new JButton("F to C");
add(button);
button.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void createButton2() {
//this is to create the Fahrenheit to Kelvin button
button2 = new JButton("F to K");
add(button2);
button2.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void createButton3() {
//this is to create the Celsius to Kelvin button
button3 = new JButton("C to K");
add(button3);
button.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void createButton4() {
//this is to create the Celsius to Fahrenheit button
button4 = new JButton("C to F");
add(button4);
button4.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void createButton5() {
//this is to create the Kelvin to Celsius button
button5 = new JButton("K to C");
add(button5);
button5.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void createButton6() {
//this is to create the Kelvin to Fahrenheit button
button6 = new JButton("K to F");
add(button6);
button6.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed
}
public void addLabels() {
add(inlabel);
add(infield);
add(outlabel);
add(outfield);
outfield.setEditable(false);//removes ability to enter a value in the output field
}
public void actionPerformed(ActionEvent i) {
int temp;
double newtemp = 0;
String inputString;
inputString = infield.getText();//pulls the number that was entered in the input field
temp = Integer.parseInt(inputString);// makes a new variable to handle the input
//tells the output how to calculate the input based on the button that was pressed
if(i.getSource() == button) {
newtemp = (5/9) * (temp - 32);//Fahrenheit to Celsius equation
}
else if(i.getSource() == button2) {
newtemp = (5/9) * (temp - 32) + 273;//Fahrenheit to Kelvin equation
}
else if(i.getSource() == button3) {
newtemp = temp + 273;//Celsius to Kelvin equation
}
else if(i.getSource() == button4) {
newtemp = ((9/5) * temp) + 32;//Celsius to Fahrenheit equation
}
else if(i.getSource() == button5) {
newtemp = temp - 273;//Kelvin to Celsius equation
}
else {
newtemp = ((temp - 273) * (9/5)) + 32;//Kelvin to Fahrenheit equation
}
outfield.setText(" "+ three.format(newtemp));
}
}
The correct values are not coming out. I am pretty sure the problem lies in the IF statements but I am lost as to how. Any help would be appreciated.