Temperature Conversion Code trouble running Java Applet

Code doesnt function when running on Java Applet

Page 1 of 1

14 Replies - 5795 Views - Last Post: 11 December 2010 - 07:27 PM Rate Topic: -----

#1 Guest_Phil*


Reputation:

Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 10:25 PM

Ive looked through a bunch of threads about converting temperatures but none are how i did it.
I use TextPad.
when i try using Run Java Applet i do not get any correct conversions

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 = ((Fahrenheit - 32) * (5.0 / 9.0));

/**
		    double Fahrenheit = Double.parseDouble(celsiusField.getText());
*/
            celsiusField.setText(Double.toString(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"))
		{

			Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
/**
			double Celsius = Double.parseDouble(fahrenheitField.getText());
*/
            fahrenheitField.setText(Double.toString(Fahrenheit));
        }
        else
           celsiusField.setText("Error in convertor code.");

      }


}



Is This A Good Question/Topic? 0

Replies To: Temperature Conversion Code trouble running Java Applet

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 10:29 PM

That's because you do your calculations with Celsius and Fahrenheit, but neither of them have any values. You need to read in some values for those two BEFORE calculating. You can do that by reading the text fields.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 10:44 PM

View PostDogstopper, on 10 December 2010 - 09:29 PM, said:

That's because you do your calculations with Celsius and Fahrenheit, but neither of them have any values. You need to read in some values for those two BEFORE calculating. You can do that by reading the text fields.

i've declared both Celsius and Fahrenheit equal to 0
Was This Post Helpful? 0

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 10:47 PM

Well, what good is that? You want it to equal whatever is in the other text area.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 10:51 PM

View PostPhil, on 10 December 2010 - 11:44 PM, said:

View PostDogstopper, on 10 December 2010 - 09:29 PM, said:

That's because you do your calculations with Celsius and Fahrenheit, but neither of them have any values. You need to read in some values for those two BEFORE calculating. You can do that by reading the text fields.

i've declared both Celsius and Fahrenheit equal to 0

And they will stay equal to zero until you translate the Ascii values enterd by the use in the JTextField to their double value
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 11:03 PM

so what do you recommend that i should do?
i've been working on this code for the past 5 hours and still no luck. i even google'd it but i havent found a java program that is written the same way i did
Was This Post Helpful? 0

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 11:10 PM

That is commented code that shouldn't probably be

/**  
             double Celsius = Double.parseDouble(fahrenheitField.getText());  
*/  


:)
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 11:21 PM

View Postpbl, on 10 December 2010 - 10:10 PM, said:

That is commented code that shouldn't probably be

/**  
             double Celsius = Double.parseDouble(fahrenheitField.getText());  
*/  


:)



if i remove the comments the code will not work at all
Was This Post Helpful? 0

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 11:30 PM

View PostPhil, on 11 December 2010 - 12:21 AM, said:

View Postpbl, on 10 December 2010 - 10:10 PM, said:

That is commented code that shouldn't probably be

/**  
             double Celsius = Double.parseDouble(fahrenheitField.getText());  
*/  


:)



if i remove the comments the code will not work at all

because you declare
double Celsius = ...
which declare a new variable Celsius which is locl to the method and will stop to exist as soon as the method exits
try

Celsius = ...
that will use the instance variable Celsius that can be referenced by all methods
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 10 December 2010 - 11:49 PM

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 Celsius;
     private 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 = ((Fahrenheit - 32) * (5.0 / 9.0));


		     Fahrenheit = Double.parseDouble(celsiusField.getText());

            celsiusField.setText(Double.toString(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"))
		{

			Fahrenheit = (Celsius * (9.0 / 5.0) + 32);

			 Celsius = Double.parseDouble(fahrenheitField.getText());

            fahrenheitField.setText(Double.toString(Fahrenheit));
        }
        else
           celsiusField.setText("Error in convertor code.");

      }


}



took out the double but just errors..
this is my first year using textpad or any java program so im not exactly sure what to do.
Was This Post Helpful? 0

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 11 December 2010 - 07:25 AM

Ok, look here and think...
			Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
			 Celsius = Double.parseDouble(fahrenheitField.getText());



What is wrong with this? Like really, look at it.

Notice that to calculate Fahrenheit, you NEED Celsius to have a value. You give it a value AFTER you calculate, not before. If you swap these two statements for both actionPerformed(), you should be ok.
Was This Post Helpful? 1
  • +
  • -

#12 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 11 December 2010 - 09:53 AM

View PostDogstopper, on 11 December 2010 - 06:25 AM, said:

Ok, look here and think...
			Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
			 Celsius = Double.parseDouble(fahrenheitField.getText());



What is wrong with this? Like really, look at it.

Notice that to calculate Fahrenheit, you NEED Celsius to have a value. You give it a value AFTER you calculate, not before. If you swap these two statements for both actionPerformed(), you should be ok.


Thanks a lot.. it works now :^:

when i put a number in the fahrenheit text field and click convert to celsius it does not work and vice versa.
i want it to be so that when i have a number in fahrenheit and i click convert to celsius the number in celsius appears. that also goes for when i put a number in celsius and i conver to fahrenheit the fahrenheit number appears.
do i have to flip flop:
celsiusField.setText(Double.toString(Celsius));
with
fahrenheitField.setText(Double.toString(Fahrenheit));



double Fahrenheit = Double.parseDouble(celsiusField.getText());

Celsius = ((Fahrenheit - 32) * (5.0 / 9.0));

celsiusField.setText(Double.toString(Celsius));


double Celsius = Double.parseDouble(fahrenheitField.getText());

Fahrenheit = (Celsius * (9.0 / 5.0) + 32);

fahrenheitField.setText(Double.toString(Fahrenheit));



Was This Post Helpful? 0

#13 Guest_Phil*


Reputation:

Re: Temperature Conversion Code trouble running Java Applet

Posted 11 December 2010 - 10:56 AM

also, is there any way i could round the results i get to 3 decimal places?
Was This Post Helpful? 0

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 11 December 2010 - 07:22 PM

View PostPhil, on 11 December 2010 - 11:56 AM, said:

also, is there any way i could round the results i get to 3 decimal places?

String str = String.format("%5.2f", doubleValue);
will conver to String doubleValue with 2 decimals
Was This Post Helpful? 0
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Temperature Conversion Code trouble running Java Applet

Posted 11 December 2010 - 07:27 PM

To round to 3 decimal places, you can use String.format():

double num = 0.888923;
		String number = String.format("%.3f ", num);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1