Hello to All, This is another newbie question. I have been working this type of error for the last couple of days and have been getting nowhere so it is time to ask you, the adoring wonders of the Java world.
How do I work doubles and chars and ints and strings together? Here is one program that has not even got off the ground because of the error. Thank you for your help, Chetanji
The reason that you can't *implicitedly* cast from a double to an integer is because you will lose stuff -- either precision and / or range. (actually, the compiler only care about range -- as you can lose precision in both directions)
However, if you really know that you won't lose stuff, or don't care if you lose stuff, you can force the compiler by *explicitedly* casting the double to an integer.
Of course, if you start getting some inaccurate answers... keep in mind that the compiler told-you-so, and you forced it anyway.
Henry [ December 02, 2007: Message edited by: Henry Wong ]
Thank you so much Henry Wong. In the beginning there are so many 'subtle' Java lessons that burn up so much personal time. But indeed this is the way it is with almost everything in life.
To be able to post a coding frustration on this blog, after personally trying almost endless different ways of re-writing, and then after applying your simply concise answer results in much debris being cleared away.
I know the best way to learn is by being thrown into the ocean and learn to swim. Learn by doing. Books and school only take you so far. You guys are providing a real time source of wonderful help, like throwing a temporary life preserver.
I hope my appreciation is not too much. Blessings, Chetan
By the way, you should not use Math.pow() to find integer powers of integers. It is a slow and inaccurate way to do the job. In some cases, you may not care about "slow", but you should always care about "inaccurate".
If you want the square of integer "x", write "x*x". If you want the cube, write "x*x*x".
If you want an arbitrary power of an integer, you could use a simple loop.
By keeping all the arithmetic as integer, the answer is guaranteed to be exactly correct (unless you overflow the range of the integer type).
For small integer powers of floats and doubles, you should also avoid Math.pow(). Certainly, it is better to square a double "d" by writing "d*d", than to use Math.pow(d, 2.0). [ December 03, 2007: Message edited by: Peter Chase ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.