![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Sexy Programmer
|
Converter, my first multiple action listener program
[PHP]
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Converter extends JApplet implements ActionListener { double celsius, fahrenheit; String input; JTextField input1, input2, output; public void init() { super.setSize(450, 200); Container panel = getContentPane(); panel.setLayout(new FlowLayout()); panel.add(new JLabel("Enter Celsius: ")); input1 = new JTextField(10); input1.addActionListener(this); panel.add(input1); // adds celsius field panel.add(new JLabel("Enter Fahrenheit: ")); input2 = new JTextField(10); input2.addActionListener(this); panel.add(input2); // adds fahrenheit field output = new JTextField(10); output.setEditable(false); panel.add(new JLabel("Convert Temperture: ")); panel.add(output); } public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getSource() == input2) //returns weird numbers! { input1.setText(""); double fahr = Double.parseDouble(input2.getText()); double newCel = convertToCelsius(fahr); output.setText(newCel + " Celsius"); } else if (actionEvent.getSource() == input1) //works { input2.setText(""); double cel = Double.parseDouble(input1.getText()); double newFahr = convertToFahrenheit(cel); output.setText(newFahr + " Fahrenheit"); } } public static double convertToCelsius(double d) { return (5.0 / 9.0) * (d - 32); } public static double convertToFahrenheit(double c) { return 9.0 / 5.0 * c + 32; } } [/PHP] when I type a number in the Fahrenheit field, the results comes up weird example: I type 1 and the result is like 222222222 Celsius. wtf is wrong with this. I have been working on it for like 1 hours debugging trying all different possibilities! but idk, someone out there help me!
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jan 2006
Posts: 17
Rep Power: 0
![]() |
public static double convertToFahrenheit(double c)
{
return (9.0 / 5.0) * c + 32;
}Order of opertaions mix up
__________________
http://img523.imageshack.us/img523/8436/user9kz.jpg |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3
![]() |
Quote:
I don't do much GUI programming in Java, but maybe the -17 is being pushed out of the viewable area. Maybe you could try reducing the number of decimal places. |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
You can use the NumberFormat class to format floating-point decimals; here is an example:
import java.text.NumberFormat;
public class NumberFormatTest {
public static void main(String[] args) {
NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setGroupingUsed(true); // add commas
nf.setMaximumFractionDigits(3); // max digits after decimal point
String formattedNumStr = nf.format( -17.2222222222D );
System.out.println(formattedNumStr); // will print -17.222
}
} |
|
|
|
|
|
#5 |
|
Sexy Programmer
|
omg! why didnt I think of that! Thanks you guys, that must be the problem! I'll inform anyone when I try to use the NumberFormat
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|