Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2006, 10:01 PM   #1
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Old Feb 22nd, 2006, 10:16 PM   #2
oNe8
Newbie
 
oNe8's Avatar
 
Join Date: Jan 2006
Posts: 17
Rep Power: 0 oNe8 is on a distinguished road
    public static double convertToFahrenheit(double c)
    {
        return (9.0 / 5.0) * c + 32;
    }

Order of opertaions mix up
oNe8 is offline   Reply With Quote
Old Feb 22nd, 2006, 10:58 PM   #3
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
Quote:
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.
Your code looks correct, and the answer should be -17.2222222222...
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.
jaeusm is offline   Reply With Quote
Old Feb 22nd, 2006, 11:22 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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
	}
}
Here are some more examples that I found useful. (Warning: Java Applet)
titaniumdecoy is offline   Reply With Quote
Old Feb 23rd, 2006, 7:01 AM   #5
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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!
ReggaetonKing is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC