Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: NEED HELP WITH TEMPERATURE

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NEED HELP WITH TEMPERATURE

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
     
    public class Temperature2 extends JApplet implements ActionListener
    {
         private JTextField fahrenheitField, celsiusField;
         private JLabel messageLabel;
         private JButton fahrButton, celsButton, resetButton;
         private double Celsius;
         private double Fahrenheit;
     
     
         public void init()
         {
            Container contentPane = getContentPane();
            JLabel fahrenheitLabel, celsiusLabel, instructionsLabel;
            JPanel fahrenheitPanel, celsiusPanel, resetPanel;
     
     
            fahrenheitPanel = new JPanel();
            celsiusPanel = new JPanel();
            resetPanel= new JPanel();
     
            instructionsLabel = new JLabel("            Temperature Converter Chart");
     
            fahrButton = new JButton("convert to fahrenheit");
    		fahrButton.addActionListener(this);
            fahrenheitPanel.add(fahrButton);
     
    		celsButton = new JButton("convert to celsius");
    		celsButton.addActionListener(this);
            celsiusPanel.add(celsButton);
     
            resetButton = new JButton("Reset");
            resetButton.addActionListener(this);
            resetPanel.add(resetButton);
     
     
            fahrenheitLabel = new JLabel("Temperature in F:");
    	    fahrenheitField = new JTextField(9);
    	    fahrenheitPanel.add(fahrenheitLabel);
    	    fahrenheitPanel.add(fahrenheitField);
     
    	    celsiusLabel = new JLabel("Temperature in C:");
    	    celsiusField = new JTextField(9);
    	    celsiusPanel.add(celsiusLabel);
    	    celsiusPanel.add(celsiusField);
     
     
    	    contentPane.setLayout(new GridLayout(6, 1));
    	    contentPane.add(instructionsLabel);
    	    contentPane.add(fahrenheitPanel);
    	    contentPane.add(celsiusPanel);
    	    contentPane.add(fahrButton);
    	    contentPane.add(celsButton);
    	    contentPane.add(resetButton);
     
         }
     
    	 public void actionPerformed(ActionEvent e)
    	 {
     
    		if (e.getActionCommand().equals("convert to celsius"))
    		{
     
                Celsius = 0;
                Fahrenheit = 0;
    		    Celsius = ((5.0 / 9.0) *  (Fahrenheit - 30));
     
     
    		 //   double Celsius = Double.parseDouble(celsiusField.getText());
     
                celsiusField.setText(Double.toString(Celsius));
                System.out.println("Temperature in Celcius is:" + Celsius);
     
    		}
     
    	else if (e.getActionCommand().equals("Reset"))
    		{
    		    Celsius = 0;
    		    Fahrenheit = 0;
    		    celsiusField.setText("0.0");
    		    fahrenheitField.setText("0.0");
     
    		}
     
    		else if (e.getActionCommand().equals("convert to fahrenheit"))
    		{
     
    			Celsius = 0;
    			Fahrenheit = 0;
    			Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
    /**
    			double Celsius = Double.parseDouble(fahrenheitField.getText());
    */
                fahrenheitField.setText(Double.toString(Fahrenheit));
                System.out.println("Temperature in Fahrenheit is:" + Fahrenheit);
            }
            else
               celsiusField.setText("Error in convertor code.");
     
          }
     
     
    }
    Last edited by helloworld922; December 11th, 2010 at 09:56 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NEED HELP WITH TEMPERATURE

    In the future please don't ask your same question in multiple categories. I've removed your other topic. What specifically are you having problems with?

    If you're getting an error message, please post it in full.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP WITH TEMPERATURE

    the problem is that there is no errors...it seems the conversion is not working properly..when i put in a number in the fahrenheit field or textbox and press the conver to celsius button the answer is always -16.666 and if i do the same and put a number in the celisus field or textbox the output for fahrenheit is always 32...

    this is the problem

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NEED HELP WITH TEMPERATURE

    Take a look at your actionPerformed method.

    When the user presses the convert to fahrenheit or convert to celsius buttons, you're initializing the values to 0 rather than to the values in the other textbox.

    0F = -16.666C and 0C = 32F.

    Change these to parse the text from textbox into a number, then convert that number to the other temperature scale.

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NEED HELP WITH TEMPERATURE

    do you mean like this..
    public void actionPerformed(ActionEvent e)
    {
      if (e.getActionCommand().equals("convert to celsius"))
     {
     
               [COLOR="Red"] 0Celsius = 32Fahrenheit;
                0Fahrenheit = -16.666Celsius;
                Celsius = ((5.0 / 9.0) *  (Fahrenheit - 30));[/COLOR]
     
              //  [COLOR="Red"]0Celsius = Double.parseDouble(celsiusField.getText());[/COLOR]
     
                celsiusField.setText(Double.toString(Celsius));
                System.out.println("Temperature in Celcius is:" + Celsius);
     
    		}
    i'm clueless
    Last edited by helloworld922; December 11th, 2010 at 02:08 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: NEED HELP WITH TEMPERATURE

    No, I was simply showing you where you were getting those numbers from. That's not valid Java code

      if (e.getActionCommand().equals("convert to celsius"))
    {
                Fahrenheit = Double.parseDouble(fahrenheit.getText());
                Celsius = ((5.0 / 9.0) *  (Fahrenheit - 30));
     
                celsiusField.setText(Double.toString(Celsius));
                //System.out.println("Temperature in Celcius is:" + Celsius); this isn't useful in a GUI app
     
    		}

    Your handler for converting Celsius to Fahrenheit will be similar to this, but you'll need to change the code around to convert celsius to fahrenheit, rather than convert fahrenheit to celsius.

Similar Threads

  1. Converting temperature program
    By ixjaybeexi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2009, 07:27 PM