jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Type mismatch:Cannot convert from double to int. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Type mismatch:Cannot convert from double to int." Watch "Type mismatch:Cannot convert from double to int." New topic

Type mismatch:Cannot convert from double to int.

Chetan Graham

Joined: Dec 01, 2007
Posts: 21
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,

Henry Wong

Joined: Sep 28, 2004
Posts: 20514

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.

[ December 02, 2007: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chetan Graham

Joined: Dec 01, 2007
Posts: 21
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Shouldn't explicitedly be explicimately?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.
I agree. Here's the link: http://aspose.com/file-tools
subject: Type mismatch:Cannot convert from double to int.
It's not a secret anymore!