Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

possible loss of precision

 
martin dewhurst
Greenhorn
Posts: 2
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags and formatted - see UseCodeTags for details]

I keep getting this message in build report
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 34410
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a careful look at this line:


What is your datatype and what is your variable name?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will that line switch (char) actually compile?
 
Tony Docherty
Bartender
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2958
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic