• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

possible loss of precision

 
Greenhorn
Posts: 2
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Added code tags and formatted - see UseCodeTags for details]

I keep getting this message in build report
 
author & internet detective
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41250
849
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a careful look at this line:


What is your datatype and what is your variable name?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will that line switch (char) actually compile?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic