Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Noob with a question about Java code  RSS feed

 
I. Ron Nie
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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:
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!