Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Centigrade to Farenheit Program Almost Compiled Except For One Error

Thread view: 
Alan Fried - 09 Dec 2004 12:08 GMT
The following program which is a awt program that converts
farenheit to centigrade and visa versa basically compiles except
at the point where I have stars on either side. It is the following
statement

double input = Double.valueOf(textField.getText());

I get the error message can not convert double to double.

Any suggestions?

Thanx in advance

Alan

import java.awt.*;
import java.awt.event.*;
class BasicFrame {
      public static void main (String [] args) {           
         
          Frame f = new Frame();
         
              f.setSize (500, 500);            
           f.setLayout(new FlowLayout());  
           Label label1 = new Label();
           Label label2 = new Label();
            label1.setText("Enter a temp then press To Celsius or To Farenheit");
            f.add(label1);
            f.add(label2);
            TextField textField = new TextField(10);
            f.add(textField);
           Button Celsius = new Button("To Celsius");
            Button Farenheit = new Button("To Farenheit");
            f.add(Celsius);
            f.add(Farenheit);
            //Celsius.addActionListener(this);
            //Farenheit.addActionListener(this);
            //Temperature therm = new Temperature();
           
            f.setVisible(true);
      }
           
                 
                   
     public void actionPerformed(ActionEvent e){
             Temperature therm = new Temperature();
             

           TextField textField = new TextField(10);
          Button Celsius = new Button("To Celsius");
        Button Farenheit = new Button("To Farenheit");
          ****    double input = Double.valueOf(textField.getText());    ****                    
           if(e.getSource()== Celsius ){
             System.out.println("Celsius Called");
           Label label2 = new Label();
             label2.setText(String.valueOf(therm.toCel(input)));
           }           
           else{
               //Farenheit.addActionListener(new ActionListener(){
                //    public void actionPerformed(ActionEvent e){
               System.out.println("Farenheit Called");
               Label label2 = new Label();
               label2.setText(String.valueOf(therm.toFar(input)));
           }
        }        
               

           
                        
 class Temperature{  
        public double toFar(double cel){         
        double far;
        far = (cel*1.8)+32;
        return far;
    }
        public double toCel(double far){
        double cel;
        cel = (far-32)/1.8;
        return cel;
 }

             

}

      }
Ryan Stewart - 09 Dec 2004 12:17 GMT
[...]
> double input = Double.valueOf(textField.getText());
>
> I get the error message can not convert double to double.
[...]
Next time copy the error message exactly as it is. It's not double and
double, it's Double and double. "Double" is a class. "double" is a primitive
type. You want either
Double input = Double.valueOf(textField.getText());

or

double input = Double.parseDouble(textField.getText());
Dan - 09 Dec 2004 12:32 GMT
> double input = Double.valueOf(textField.getText());

Double with a capital D!!!!!!
Bet ur kickin urself now! ;)
Dan
klynn47@comcast.net - 09 Dec 2004 13:23 GMT
The problem is that the valueOf method produces a Double. What you need
to use is either the parseDouble method or create a new Double and use
doubleValue()
Alan Fried - 10 Dec 2004 00:00 GMT
Thank you all for finally helping me at lest compile
the program correctly. However when I run it
I get a window with all the correct buttons and
prompts. However when I enter a temperature
the program freezes. Any suggestions?

Thanx in advance

Alan

Here is the code:

import java.awt.*;
import java.awt.event.*;
class BasicFrame {
      public static void main (String [] args) {           
         
          Frame f = new Frame();
         
              f.setSize (500, 500);            
           f.setLayout(new FlowLayout());  
           Label label1 = new Label();
           Label label2 = new Label();
            label1.setText("Enter a temp then press To Celsius or To Farenheit");
            f.add(label1);
            f.add(label2);
            TextField textField = new TextField(10);
            f.add(textField);
           Button Celsius = new Button("To Celsius");
            Button Farenheit = new Button("To Farenheit");
            f.add(Celsius);
            f.add(Farenheit);
            //Celsius.addActionListener(this);
            //Farenheit.addActionListener(this);
            //Temperature therm = new Temperature();
           
            f.setVisible(true);
      }
           
                 
           public void actionPerformed(ActionEvent e){
             Temperature therm = new Temperature();
             

           TextField textField = new TextField(10);
          Button Celsius = new Button("To Celsius");
        Button Farenheit = new Button("To Farenheit");
              double input = Double.parseDouble(textField.getText());                       
           if(e.getSource()== Celsius ){
             System.out.println("Celsius Called");
           Label label2 = new Label();
             label2.setText(String.valueOf(therm.toCel(input)));
           }           
           else{
               //Farenheit.addActionListener(new ActionListener(){
                //    public void actionPerformed(ActionEvent e){
               System.out.println("Farenheit Called");
               Label label2 = new Label();
               label2.setText(String.valueOf(therm.toFar(input)));
           }
        }        
               

           
                        
 class Temperature{  
        public double toFar(double cel){         
        double far;
        far = (cel*1.8)+32;
        return far;
    }
        public double toCel(double far){
        double cel;
        cel = (far-32)/1.8;
        return cel;
 }

             

}

      }
Bjorn Abelli - 10 Dec 2004 01:06 GMT
> Thank you all for finally helping me at lest compile
> the program correctly. However when I run it
> I get a window with all the correct buttons and
> prompts. However when I enter a temperature
> the program freezes. Any suggestions?

It doesn't "freeze". I just doesn't work...

First, let BasicFrame extend Frame and implement ActionListener.

  class BasicFrame
  extends java.awt.Frame
  implements ActionListener

Second, in the main method, just instantiate a BasicFrame:

  BasicFrame bf = new BasicFrame();

To be able to use the actionPerformed-method, you have to work with an
instance of a class where you can create such a method. In the code you
submitted, you never instantiated BasicFrame, which in your code even wasn't
an ActionListener...

Third, what you *now* have in the main-method, you put into a constructor
for BasicFrame instead. However, let the Buttons and the TextField be
instance variables instead of local variables, as you'll need to reach them
later, i.e. put the instantiation of those outside of the constructor...

Fourth, uncomment the "addActionListener"-sentences in your constructor,
which now will work, as "this" now will reference an instance, since it is
in the constructor of a BasicFrame.

Fifth, scratch all the redundant code in actionPerformed (why are you even
*trying* to instantiate a new set of buttons and a textfield?), because you
want to check if you pressed the *instance* buttons, not the local ones.

// Bjorn A
Bjorn Abelli - 10 Dec 2004 01:15 GMT
"Bjorn Abelli" wrote...

> First, let BasicFrame extend Frame and implement ActionListener.

> Second, in the main method, just instantiate a BasicFrame:

> Third, what you *now* have in the main-method, you put into a constructor
> for BasicFrame instead.

> Fourth, uncomment the "addActionListener"-sentences in your constructor,

> Fifth, scratch all the redundant code in actionPerformed

Sixth, you should also look into the implementation of WindowListeners, so
you can close the Frame nicely...

Alternatively, look into using JFrames, JButtons, etc. from javax.swing
instead of the controls from java.awt.

// Bjorn A
Alan Fried - 11 Dec 2004 01:48 GMT
>"Bjorn Abelli" wrote...
>
[quoted text clipped - 8 lines]
>
>> Fifth, scratch all the redundant code in actionPerformed

Bjorn thank you so much for your help and I did as you said but I
get a compile error on:

class BasicFrame extends Frame implements ActionListener{

The serializable class BasicFrame does not declare a static final serialVersionUID
field of type long.

After I run the program and type in a number I get:

GetModuleHandleA succeed

LoadLibrary returns baaa0000
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at BasicFrame.actionPerformed(Temperature4.java:38)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

import java.awt.*;
import java.awt.event.*;
class AWTWorld {
      public static void main (String [] args) {           
         
        BasicFrame bf = new BasicFrame();
          bf.setVisible(true);       
                        
      }
}           

class BasicFrame extends Frame implements ActionListener{
       
    static TextField textField = new TextField(10);
    static Button Celsius = new Button("To Celsius");
    static Button Farenheit = new Button("To Farenheit");

        public BasicFrame(){
       Temperature therm = new Temperature();
        setSize (500, 500);            
         setLayout(new FlowLayout());  
          Label label1 = new Label();
         Label label2 = new Label();
          label1.setText("Enter a temp then press To Celsius or To Farenheit");
          add(label1);
          add(label2);
          TextField textField = new TextField(10);
          add(textField);
         Button Celsius = new Button("To Celsius");
          Button Farenheit = new Button("To Farenheit");
          add(Celsius);
          add(Farenheit);
        Celsius.addActionListener(this);
          Farenheit.addActionListener(this);
      }
        public void actionPerformed(ActionEvent e){
        double input = Double.parseDouble(textField.getText());                       
           if(e.getSource()== Celsius ){
             System.out.println("Celsius Called");
           Label label2 = new Label();
             label2.setText(String.valueOf(therm.toCel(input)));
           }           
           else{
               //Farenheit.addActionListener(new ActionListener(){
                //    public void actionPerformed(ActionEvent e){
               System.out.println("Farenheit Called");
               Label label2 = new Label();
               label2.setText(String.valueOf(therm.toFar(input)));
           }
        }        
               

           
                        
 class Temperature{  
        public double toFar(double cel){         
        double far;
        far = (cel*1.8)+32;
        return far;
    }
        public double toCel(double far){
        double cel;
        cel = (far-32)/1.8;
        return cel;
 }

             

 }

}
Bjorn Abelli - 11 Dec 2004 02:40 GMT
"Alan Fried" wrote...

> Bjorn thank you so much for your help
> and I did as you said

Well, not *exactly* as I said, and you did some other changes as well... ;-)

You *still* create *local* variables for buttons and textfields instead of
using the instance variables...

And you didn't make them *instance* variables either, but *class* variables.
Ok, that would also work in this particular case, but try to think about the
difference between them, as you will need to know the difference in the
future.

Eliminate the local versions of textField, Celcius and Farenheit in all of
your methods. And by the way, make the instance of Temperature, therm,  an
instance variable as well...

> but I get a compile error on:
>
> class BasicFrame extends Frame implements ActionListener{
>
> The serializable class BasicFrame does not declare
> a static final serialVersionUID field of type long.

I didn't get that error, so there is probably something special setting at
your machine...

But you're correct in saying that your code won't compile!

In *your* code the variable "therm" isn't reachable from within the method
actionPerformed, as it is a *local* variable inside the constructor. I
repeat, lift that as well *out* of the method, and make it an instance
variable (or a class variable with static if you must).

That you even can execute is because there are some old versions of the
classes around...

The cause of that error is simply because the textField you try to parse
*is* empty, which the error says in plain text.

> Exception in thread "AWT-EventQueue-0"
>    java.lang.NumberFormatException: empty String

...and that is just because of what I told you in previous messages and as I
repeated above; don't duplicate the textfield and buttons as local
variables. What happens is that you're trying to parse the textfield that is
*local* inside the method, not the one on the form...

// Bjorn A
Alan Fried - 11 Dec 2004 16:27 GMT
>"Alan Fried" wrote...
>
[quoted text clipped - 5 lines]
>You *still* create *local* variables for buttons and textfields instead of
>using the instance variables...

Yes that was the problem it now runs correctly!

Thanx for your help
Roy Goodman - 13 Dec 2004 02:34 GMT
> Thank you all for finally helping me at lest compile
> the program correctly. However when I run it
> I get a window with all the correct buttons and
> prompts. However when I enter a temperature
> the program freezes. Any suggestions?

Maybe try entering a warmer temperature?

Sorry, I just couldn't help myself.  Or anyone else I guess.

I'll go away now ....

Roy
Tony Morris - 11 Dec 2004 02:24 GMT
I did that a few years ago if you're interested.

http://www.xdweb.net/~dibblego/utilities/temperature

Signature

Tony Morris
http://xdweb.net/~dibblego/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.