wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes possible loss of precision Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "possible loss of precision" Watch "possible loss of precision" New topic
Author

possible loss of precision

martin dewhurst
Greenhorn

Joined: Sep 23, 2012
Posts: 2

[Added code tags and formatted - see UseCodeTags for details]

I keep getting this message in build report
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Martin,
Welcome to CodeRanch!

This line is giving you trouble:



The problem is that num1 and num2 are doubles which sum up to a double. The compiler is concerned what to do about the decimal part of that double since it can't be put in an integer. You can solve it by making anser of type double or casting


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jeanne's suggestion will get rid of the error and allow your code to compile, but before you do that, you need to ask youself why num1 and num2 are doubles while answer is an int. Do you have a good reason for starting with two doubles, adding them, and then dropping the fractional part? Or would it make more sense to make num1 and num2 ints, or to make answer a double?
martin dewhurst
Greenhorn

Joined: Sep 23, 2012
Posts: 2



tried this and got 1 error , i am real new to this and im finding it tricky , this forum and the help is greatly appreciated

[MODERATOR ACTION: Added code tags.]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30753
    
156

Take a careful look at this line:


What is your datatype and what is your variable name?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Will that line switch (char) actually compile?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2315
    
  49
Also you are asking the user to enter a number but not bothering to store it anywhere and then you are asking them to enter a letter and are trying to convert it to a double.
You should always check the value returned by the input dialog before using it in case the user cancelled the dialog, in which case the input dialog will return null.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2315
    
  49
And another thing, why do you have double variables called S, A, M & D.
You should always give variables meaningful names and use the Java naming conventions (variable names start with a lower case letter).
What do these variables do? you never assign values to them but use them in a comparison with choice and character.

It looks like you have become confused over how to test for which math operator the user wants to use and have ended up combining multiple solutions. I suggest you sit down with a pen and paper and write out in words how you want to solve the problem before trying to code it.

tried this and got 1 error

Are you sure, there are loads of compilations errors in this code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: possible loss of precision