First of all, I´m new to both Java and programming (except Matlab), so simple answers is much appreciated :-).
I'm trying to create a temperature converter (with GUI) and I need to update some labels. That worked fine in the beginning, but now I have to use values from inside an if-statement. This results in an error where I try to update the labels:
tempKelvin cannot be resolved to a variable
All the action happens when the "Convert" button is clicked, the code for this is here:
// Create and add convert button
JButton fahrenheitButton = new JButton("Convert");
fahrenheitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Check if input is of type double and perform action
if (isNumber(tempTextField.getText())) {
double inputTemp = Double.parseDouble(tempTextField.getText());
// Convert from Kelvin
if (((String) unitDropdown.getSelectedItem()).equals("Kelvin")) {
int tempKelvin = (int) (inputTemp);
int tempCelcius = (int) (inputTemp - 273.15);
int tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5) + 32);
// Convert from Celsius
} else if (((String) unitDropdown.getSelectedItem()).equals("Celsius")) {
int tempKelvin = (int) (inputTemp + 273.15);
int tempCelcius = (int) (inputTemp);
int tempFahrenheit = (int) (inputTemp * (9/5) + 32);
// Convert from Fahrenheit
} else if (((String) unitDropdown.getSelectedItem()).equals("Fahrenheit")) {
int tempKelvin = (int) ((inputTemp - 32) * (5/9) + 273.15);
int tempCelcius = (int) ((inputTemp - 32) * (5/9));
int tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5) + 32);
// If none of the above was selected, it's an error...
} else {
int tempKelvin = 0;
int tempCelcius = 0;
int tempFahrenheit = 0;
warningLabel.setText("Oops, this doesn't look good!");
}
// Update labels
kelvinLabel.setText(tempKelvin + " K");
celsiusLabel.setText(tempCelcius + " C");
fahrenheitLabel.setText(tempFahrenheit + " F");
warningLabel.setText("");
} else {
warningLabel.setText("Input must be numeric!");
}
}
});
fahrenheitButton.setBounds(20, 45, 89, 23);
contentPane.add(fahrenheitButton);
Any help would be greatly appreciated, thanks!!
YOu need to define int tempKelvin outside the if statements and reuse it as mentioned here:
// Create and add convert button
JButton fahrenheitButton = new JButton("Convert");
fahrenheitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Check if input is of type double and perform action
if (isNumber(tempTextField.getText())) {
double inputTemp = Double.parseDouble(tempTextField.getText());
int tempKelvin = -1;
int tempCelcius = -1;
int tempFahrenheit = -1;
// Convert from Kelvin
if (((String) unitDropdown.getSelectedItem()).equals("Kelvin")) {
tempKelvin = (int) (inputTemp);
tempCelcius = (int) (inputTemp - 273.15);
tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5) + 32);
// Convert from Celsius
} else if (((String) unitDropdown.getSelectedItem()).equals("Celsius")) {
tempKelvin = (int) (inputTemp + 273.15);
tempCelcius = (int) (inputTemp);
tempFahrenheit = (int) (inputTemp * (9/5) + 32);
// Convert from Fahrenheit
} else if (((String) unitDropdown.getSelectedItem()).equals("Fahrenheit")) {
tempKelvin = (int) ((inputTemp - 32) * (5/9) + 273.15);
tempCelcius = (int) ((inputTemp - 32) * (5/9));
tempFahrenheit = (int) ((inputTemp - 273.15) * (9/5) + 32);
// If none of the above was selected, it's an error...
} else {
tempKelvin = 0;
tempCelcius = 0;
tempFahrenheit = 0;
warningLabel.setText("Oops, this doesn't look good!");
}
// Update labels
kelvinLabel.setText(tempKelvin + " K");
celsiusLabel.setText(tempCelcius + " C");
fahrenheitLabel.setText(tempFahrenheit + " F");
warningLabel.setText("");
} else {
warningLabel.setText("Input must be numeric!");
}
}
});
fahrenheitButton.setBounds(20, 45, 89, 23);
contentPane.add(fahrenheitButton);