 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
George Hester Guest
|
Posted: Sat Apr 03, 2004 4:53 am Post subject: operator * cannot be applied |
|
|
Here is a piece of CurrencyConvertor.java
//private static final double EXCHANGE_RATE = 0.613;
try
{
NumberFormat nf = NumberFormat.getInstance();
double poundValue = nf.parse(amount).doubleValue();
//poundValue *= EXCHANGE_RATE;
String rate = getInitParameter("RATE");
poundValue *= rate;
RATE is part of a <init-param> element in a web.xml file for my Tomcat Javaserver.
<init-param>
<param-name>RATE</param-name>
<param-value>0.65</param-value>
</init-param>
If I remove the above comments // and comment the appropriate statements there is no issue. Everything compiles and runs as it should. But as you see it here the CurrencyConvertor.java does NOT compile. The error is:
C:Documents and SettingsAdministratorMy DocumentsVisual Studio ProjectsCurr
encyConverter>javac CurrencyConverter.java
CurrencyConverter.java:31: operator * cannot be applied to double,java.lang.Stri
ng
poundValue *= rate;
^
1 error
What have I done wrong?
J2SDK1.4.2
--
George Hester
__________________________________
|
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Apr 03, 2004 5:25 am Post subject: Re: operator * cannot be applied |
|
|
On Sat, 03 Apr 2004 04:53:01 GMT, "George Hester"
<hesterloli (AT) hotmail (DOT) com> wrote or quoted :
| Quote: | NumberFormat nf =3D NumberFormat.getInstance();
double poundValue =3D nf.parse(amount).doubleValue();
//poundValue *=3D EXCHANGE_RATE;
String rate =3D getInitParameter("RATE");
|
You are doing something screwy here needlessly encoding your
characters. You may have some MIME type option set in a peculiar way.
It is pretty hard to figure out what you are talking about with all
those =3Ds in there.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
| Back to top |
|
 |
George Hester Guest
|
Posted: Sat Apr 03, 2004 6:13 am Post subject: Re: operator * cannot be applied |
|
|
That is your newsreader not me. Sorry.
George Hester
__________________________________
"Roedy Green" wrote in message news:3jis60lkrvu4bcticmjighj79dm2boa81h (AT) 4ax (DOT) com...
| Quote: | On Sat, 03 Apr 2004 04:53:01 GMT, "George Hester"
[email]hesterloli (AT) hotmail (DOT) com[/email]> wrote or quoted :
NumberFormat nf =3D NumberFormat.getInstance();
double poundValue =3D nf.parse(amount).doubleValue();
//poundValue *=3D EXCHANGE_RATE;
String rate =3D getInitParameter("RATE");
You are doing something screwy here needlessly encoding your
characters. You may have some MIME type option set in a peculiar way.
It is pretty hard to figure out what you are talking about with all
those =3Ds in there.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
|
| Back to top |
|
 |
George Hester Guest
|
Posted: Sat Apr 03, 2004 6:16 am Post subject: Re: operator * cannot be applied |
|
|
Here's a zip of the question just copied and pasted from the original. It should display correctly in Notpad.
--
George Hester
__________________________________
"Roedy Green" <look-at-the-website (AT) mindprod (DOT) com> wrote
| Quote: | On Sat, 03 Apr 2004 04:53:01 GMT, "George Hester"
[email]hesterloli (AT) hotmail (DOT) com[/email]> wrote or quoted :
NumberFormat nf =3D NumberFormat.getInstance();
double poundValue =3D nf.parse(amount).doubleValue();
//poundValue *=3D EXCHANGE_RATE;
String rate =3D getInitParameter("RATE");
You are doing something screwy here needlessly encoding your
characters. You may have some MIME type option set in a peculiar way.
It is pretty hard to figure out what you are talking about with all
those =3Ds in there.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
|
| Back to top |
|
 |
George Hester Guest
|
Posted: Sat Apr 03, 2004 7:03 am Post subject: Re: operator * cannot be applied |
|
|
No need to check the source. I figured it out. The issue is casting a string to a double.
The example I had was incorrect. It was like this:
NumberFormat nf = NumberFormat.getInstance();
double poundValue = nf.parse(amount).doubleValue();
String rate = getInitParameter("RATE");
poundValue *= rate;
This was wrong. It should be:
NumberFormat nf = NumberFormat.getInstance();
double poundValue = nf.parse(amount).doubleValue();
String rate = getInitParameter("RATE");
double dblrate = nf.parse(rate).doubleValue();
poundValue *= dblrate;
That casted the string rate to a double dblrate. Which is what the trouble was.
George Hester
__________________________________
"George Hester" <hesterloli (AT) hotmail (DOT) com> wrote
Here's a zip of the question just copied and pasted from the original. It should display correctly in Notpad.
--
George Hester
__________________________________
"Roedy Green" <look-at-the-website (AT) mindprod (DOT) com> wrote
| Quote: | On Sat, 03 Apr 2004 04:53:01 GMT, "George Hester"
[email]hesterloli (AT) hotmail (DOT) com[/email]> wrote or quoted :
NumberFormat nf =3D NumberFormat.getInstance();
double poundValue =3D nf.parse(amount).doubleValue();
//poundValue *=3D EXCHANGE_RATE;
String rate =3D getInitParameter("RATE");
You are doing something screwy here needlessly encoding your
characters. You may have some MIME type option set in a peculiar way.
It is pretty hard to figure out what you are talking about with all
those =3Ds in there.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
|
|
|
| Back to top |
|
 |
Ryan Stewart Guest
|
Posted: Sat Apr 03, 2004 1:22 pm Post subject: Re: operator * cannot be applied |
|
|
"Mark Haase" <mehaase (AT) earthlink (DOT) net> wrote
| Quote: | If rate holds a number in it, you'll need to convert it explicitly by
using, for instance, String.parseDouble(), before you can perform
arithmetic operations with it.
|
Double.parseDouble() ?
|
|
| Back to top |
|
 |
George Hester Guest
|
Posted: Sat Apr 03, 2004 3:23 pm Post subject: Re: operator * cannot be applied |
|
|
I did something like that and fixed it. This was an example verbatum from a book "Tomcat Kick Start" by Martin Bond and Debbie Law. Why do writers of Computer books publish code which doesn't work?
George Hester
__________________________________
"Mark Haase" wrote in message news:mehaase-701B51.02041203042004 (AT) netnews (DOT) upenn.edu...
| Quote: | In article <Nqrbc.9469$LW2.5478 (AT) twister (DOT) nyroc.rr.com>,
"George Hester" <hesterloli (AT) hotmail (DOT) com> wrote:
If I remove the above comments // and comment the appropriate statements
there is no issue. Everything compiles and runs as it should. But as you
see it here the CurrencyConvertor.java does NOT compile. The error is:
I doubt this. EXCHANGE RATE is not a valid name for a variable.
C:Documents and SettingsAdministratorMy DocumentsVisual Studio
ProjectsCurr
encyConverter>javac CurrencyConverter.java
CurrencyConverter.java:31: operator * cannot be applied to
double,java.lang.Stri
ng
poundValue *= rate;
^
1 error
What have I done wrong?
J2SDK1.4.2
Did you even read the error? It says you can't multiply a double times a
String...which should be intuitively obvious.
If rate holds a number in it, you'll need to convert it explicitly by
using, for instance, String.parseDouble(), before you can perform
arithmetic operations with it.
--
|/| /| |2 |
mehaase(at)sas(dot)upenn(dot)edu
|
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Sun Apr 04, 2004 2:19 pm Post subject: Re: operator * cannot be applied |
|
|
George Hester wrote:
| Quote: | NumberFormat nf = NumberFormat.getInstance();
double poundValue = nf.parse(amount).doubleValue();
String rate = getInitParameter("RATE");
double dblrate = nf.parse(rate).doubleValue();
poundValue *= dblrate;
That casted the string rate to a double dblrate.
|
Just to be picky about terminology; that did not cast the String to a
double. Casting a String to a double is, in fact, impossible and always
results in a compiler error. What it did was convert a String to a
double.
The word "cast" -- at least in Java, and in a number of other languages
-- refers to a specific piece of language syntax -- namely:
( type ) expr
Hopefully, that will save you some confusion in the future.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
George Hester Guest
|
Posted: Sun Apr 04, 2004 7:06 pm Post subject: Re: operator * cannot be applied |
|
|
Thanks Chris. Actually I knew that. I used "Cast" to see if anyone would clarify it for me. I knew I used the expression incorrectly but I didn't know why. Thanks again.
George Hester
__________________________________
"Chris Smith" wrote in message news:MPG.1ad85d78e3424af69899c6 (AT) news (DOT) pop4.net...
| Quote: | George Hester wrote:
NumberFormat nf = NumberFormat.getInstance();
double poundValue = nf.parse(amount).doubleValue();
String rate = getInitParameter("RATE");
double dblrate = nf.parse(rate).doubleValue();
poundValue *= dblrate;
That casted the string rate to a double dblrate.
Just to be picky about terminology; that did not cast the String to a
double. Casting a String to a double is, in fact, impossible and always
results in a compiler error. What it did was convert a String to a
double.
The word "cast" -- at least in Java, and in a number of other languages
-- refers to a specific piece of language syntax -- namely:
( type ) expr
Hopefully, that will save you some confusion in the future.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|