• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Type mismatch:Cannot convert from double to int.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Chetan Graham
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't explicitedly be explicimately?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic