Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Wrapper

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Henas Cirtak
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I call the java.lang.Double class? I thought that it was automatically imported.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or perhaps the example in your book defines a method doubleValue somewhere else, could be something like -

double doubleValue(Double d){ return d.doubleValue();}
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic