aspose file tools*
The moose likes Beginning Java and the fly likes Wrapper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper" Watch "Wrapper" New topic
Author

Wrapper

Henas Cirtak
Greenhorn

Joined: Oct 02, 2004
Posts: 10
I'm trying to convert a String to a double value. My book says to first create a Double object and then convert the Double to a double value. It's not working... anybody got any ideas?

Thanks!


import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class CalcPay extends Applet implements ActionListener
{
TextField text1 = new TextField("hours worked",15);
TextField text2 = new TextField("hourly rate", 15);
Button button = new Button("Press Me");
Label label = new Label(" ");

public void init()
{
add(text1);
add(text2);
add(button);
add(label);
button.addActionListener(this);
invalidate();
validate();
}

public void actionPerformed(ActionEvent e)
{
String text1Number = text1.getText();
String text2Number = text2.getText();

Double text1Double = Double.valueOf(text1Number);
Double text2Double = Double.valueOf(text2Number);

double text1double = doubleValue(text1Double);
double text2double = doubleValue(text2Double);

}
}
Henas Cirtak
Greenhorn

Joined: Oct 02, 2004
Posts: 10
This is the error that I get...

C:\Documents and Settings\Patrick\My Documents\Java Programs\Chapter 6\CalcPay\CalcPay.java:31: cannot resolve symbol
symbol : method doubleValue (java.lang.Double)
location: class CalcPay
double text1double = doubleValue(text1Double);
^
C:\Documents and Settings\Patrick\My Documents\Java Programs\Chapter 6\CalcPay\CalcPay.java:32: cannot resolve symbol
symbol : method doubleValue (java.lang.Double)
location: class CalcPay
double text2double = doubleValue(text2Double);
^
2 errors

Tool completed with exit code 1
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Originally posted by Henas Cirtak:

double text1double = doubleValue(text1Double);
double text2double = doubleValue(text2Double);



Have you defined the doubleValue() method somewhere else that you are calling?
If you havent, then perhaps what you wanted to do was call the doubleValue method defined in java.lang.Double class.

Also have a look at parseDouble method defined in the same class.

cheers.
Sonny


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Henas Cirtak
Greenhorn

Joined: Oct 02, 2004
Posts: 10
How do I call the java.lang.Double class? I thought that it was automatically imported.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Henas,

How do I call the java.lang.Double class? I thought that it was automatically imported.

Yes. java.lang package is automatically imported. So you don't have to import it.



Better version...


Joyce
[ October 13, 2004: Message edited by: Joyce Lee ]
Henas Cirtak
Greenhorn

Joined: Oct 02, 2004
Posts: 10
Thank you I got it to work! Although, my book gives the example...

String stringValue = new String("147.82");
Double tempValue = Double.valueOf(stringValue);
double doubleValue = doubleValue(tempValue);

Am I just missing what the book is trying to teach or is it wrong?
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Am I just missing what the book is trying to teach or is it wrong?
It could be a typo error. You might want to check out the book's errata.

Joyce
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

or perhaps the example in your book defines a method doubleValue somewhere else, could be something like -

double doubleValue(Double d){ return d.doubleValue();}
Dun Dagda
Ranch Hand

Joined: Oct 12, 2004
Posts: 54
Hi,
unless the doubleValue method is defined somewhere else, I suggest the code given in your textbook example is a typo. It probably should read something like:

where you use the valueOf method to return a Double object, tempValue, then you call its doubleValue() method to return a double primitive.
However the examples already posted above by others using the static parseDouble(String s) method would probably work more efficiently.
Happy Coding!
Dun Dagda


SCJP 1.4<br />SCWCD (in progress)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper