• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Noob with a question about Java code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I'm new to Java and programming. This question is probably really stupid for experienced programmers.

I'm writing a practice code to tell me the value of an investment after one year of 40% growth. I declared two interger variables for the initial investment, and for the one year later investment. Then I wrote the following

deltainvest = invest + (invest * 0.4);

When compiling, I get the following message:

possible loss of precision
found : double
required : int

and then there is a "carrot" displayed under the plus sign in the above line of code.

Can anyone tell me what I am doing wrong? thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. Welcome to JavaRanch!

You're using integer variables, so they can't cope with anything after the decimal point. So the 0.4 is a problem. 0.4 is a double, so any calculation involving it is also going to give a double.

So when you try and assign that to an integer, you are potentially throwing away everything after the decimal point. The compiler won't let you do that without explicitly saying so. You potentially have this problem every time you convert from a "bigger" to a "smaller" type.

So you've got two options. One, you could use double variables throughout. Or, you can add an explicit cast like this:This is telling the compiler "yes, I know I'm going to lose information, and I'm happy with that".

By the way, the calculation doesn't look right, based on what you've described. I'd expect one of the following:
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that "invest + (invest * 0.4);" returns a double because 0.4 is a double. And you're trying to store it in an int which is smaller. That results in loss of precision just like the error said. To accept that loss you need to cast.

deltainvest = (int) invest + (invest * 0.4);
 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, guys!! Thanks for your very helpful and complete reply.

1) I understand the problem and the suggested fixes. I'll give it a try.
2) What is a double? Is that a primitive data type?
3) I'm thinking that the code I'm writing will tell me the initial investment plus the 40% added. So: invest + 40% of invest.

Thanks again for your very complete reply. I'll probably have lots of these questions in the weeks to come. I appreciate your patience with stupid questions. Ron.


Matthew Brown wrote:Hi. Welcome to JavaRanch!

You're using integer variables, so they can't cope with anything after the decimal point. So the 0.4 is a problem. 0.4 is a double, so any calculation involving it is also going to give a double.

So when you try and assign that to an integer, you are potentially throwing away everything after the decimal point. The compiler won't let you do that without explicitly saying so. You potentially have this problem every time you convert from a "bigger" to a "smaller" type.

So you've got two options. One, you could use double variables throughout. Or, you can add an explicit cast like this:This is telling the compiler "yes, I know I'm going to lose information, and I'm happy with that".

By the way, the calculation doesn't look right, based on what you've described. I'd expect one of the following:

 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works!!! (grin)

Here is the full code if anyone else wants to try it out:

class Testa {
public static void main(String[] arguments) {
int invest = 14000;
int deltainvest1;
System.out.println("Your initial investment is: " + invest);
deltainvest1 = (int)(invest + (invest * 0.4));
System.out.println("Your initial investment after one year of 40% return is: $" + deltainvest1);

deltainvest1 = deltainvest1 - 1500;
System.out.println ("If your investment loses $1500 in the second year, your return is $" +deltainvest1);

deltainvest1 = (int)(deltainvest1 + (deltainvest1 * 0.12));
System.out.println("If your investment gains 12% in the third year, your total return is $" + deltainvest1);

}
}


Thanks all who helped. ron
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic