Celsius to Fahrenheit in Java
I can convert from fahrenheit to celcius, but not the other way around. I have attached the code below. Hopefully it's enough to see what is going on.
public void actionPerformed(ActionEvent arg0) {
double temps = 0, temp1 = 0;
String instrings;
instrings = temp.getText();
if(instrings.equals(""))
{
instrings = "0";
temp.setText("0");
}
temps = Double.parseDouble(instrings);
instrings = temp.getText();
if(instrings.equals(""))
{
instrings = "0";
temp.setText("0");
}
temp1 = Double.parseDouble(instrings);
if(arg0.getActionCommand().equals("C")){
temps = (( temps * 9)/5+32);
DecimalFormat formatters = new DecimalFormat("#,###,###.###");
results.setText(""+formatters.format(temps));
}
else if(arg0.getActionCommand().equals("F"));
{
temp1 = (((temps - 32)/9)*5);
DecimalFormat formatters = new DecimalFormat("#,###,###.###");
results.setText(""+formatter开发者_StackOverflow中文版s.format(temp1));
}
}
Put a
System.out.println(arg0.getActionCommand());
in your method, and supposedly you will see that it is not "C"
when using the Celsius button.
Generally, look at the console to see if there are any error messages around.
Another idea: You are using two double variables temps
and temp1
, parsing the instrings
into both of them, and setting then one of them to the result. Why so complicated?
In general, I would recommend to not use the getActionCommand()
method, but to give each button an own ActionListener. You can use an anonymous class for this. As both cases do almost the same, use two subclasses of a inner (or even local) class here:
abstract class ConversionListener implement ActionListener {
DecimalFormat formatter = new DecimalFormat("#,###,###.##");
public void actionPerformed(ActionEvent e) {
String input = temp.getText();
if (input.equals("")) {
input = "0";
temp.setText(input);
}
double number = Double.parseDouble(input);
results.setText(formatter.format(convert(number)));
}
/** to implement by subclasses */
abstract double convert(double number);
}
celsiusToFahrenheit.addActionListener(new ConversionListener() {
double convert(double celsius) {
return number * 9 / 5 + 32;
}
});
fahrenheitToCelsius.addActionListener(new ConversionListener() {
double convert(double fahrenheit) {
return (fahrenheit - 32) / 9 * 5;
}
});
Not sure what you are having trouble with but here is how to convert Celsius to Fahrenheit in Java.
float celsius = celsius value
float c=Float.parseFloat(celsius);
float f = 32 + 9*c/5;
Change from
temp1 = (((temps - 32)/9)*5);
to
temp1 = (((temps - 32)*5)/9);
instead.
On this part:
instrings = temp.getText();
if(instrings.equals(""))
{
instrings = "0";
temp.setText("0");
}
temps = Double.parseDouble(instrings);
instrings = temp.getText();
if(instrings.equals(""))
{
instrings = "0";
temp.setText("0");
}
temp1 = Double.parseDouble(instrings);
Am I missing something or is temps and temp1 both getting set to the same value? Or is it possible that between the temps assignment the temp.getText() could return a different value?
If it's the same, start by rewriting it to
instrings = temp.getText();
if(instrings.equals(""))
{
instrings = "0";
temp.setText("0");
}
temps = Double.parseDouble(instrings);
temp1 = Double.parseDouble(instrings);
or even temp1 = temps
Also, some better naming of variables might be easier, temps, temp and temp1 are all very similar. One appears to be text from a control and the other 2 are numeric values.
Even names like 'inputTemperature' and 'calculatedTemperature' go a long way to making the code more readable.
This doesn't help solve your problem, but might help debugging in the future.
精彩评论