# Incompatible types: possible lossy conversion from double to int

689 views

Problem :

I am facing the below error in my program saying:

``````Lyrics.java:11: error: cannot find symbol

Cube(b);
^
symbol: method Cube(int)

location: class Lyrics

Lyrics.java:15: error: incompatible types: possible lossy conversion from double to int

return Math.pow (b, 3);
^
2 errors``````

I did research on my issue and it turns out that other people also had the same issue but none of them have solution on it. And also I am unable to understand the above error saying cannot find symbol. Please help me I am really been stuck on this error for a while.

## Solution :

The very first problem I can see is the simple typo. Java is case sensitive language, so “cube” and “Cube” mean completely different things. Solution: 1) Please be consistent, and 2) Please use names starting with the lowercase letters for method names as per your Java style guides.

The second problem is due to your method signature for a Math.pow; please see the javadoc. It seems it returns the result as the double. You then try to return the double as the int, and that is a incorrect conversion.

So the solutions is as below:

``````return b * b * b;   // i.e. please don't use `pow`.

or

return (int) Math.pow(b, 3);``````

Hope this solution solves your problem.

## Solution:

At the time you convert `double` to `int`,the precision of the value is lost. For example, at the time you convert 4.8657 (double) to int.The int value will be 4.Primitive `int` does not stocks decimal numbers.Hence you will lose 0.8657.

In your instance,0.7 is a double value(floating point employ as double by default if not mentioned as float-0.7f). At the time you calculate `price*much*0.7` ,the answer is a double value and hence the compiler wouldn't approve you to store it in a type integer because there could be a loss of precision. Hence that's what is `possible lossy conversion`,you may lose precision.

So what could you do about it? You require to tell the compiler that you truly want to do it.You require to tell it that you know what you are doing. Therefore explicitly convert double to int employing the following code:

``````int total2= (int) price*much*0.7;
/*(int) tells compiler that you are aware      of what you are doing.*/
//also called as type casting``````

In your instance,because you are calculating the cost,I'll suggest you to declare variable `total2` as the type double or float.

``````double total2=price*much*0.7;
float total2=price*much*0.7;
//will work``````

There are many things wrong with that code, however the error you are getting is here:

``amount = horsePower*0.746*rate*numberHours;``

`amount` is an integer, however the result of the expression on the right is a double (since the presence of floating point data "0.746" and "`rate`" implies that it all gets "promoted" to floating point even if "`horsePower`" and "`numberHours`" are integer - it it wasn't then the floating point would have to be truncated to an integer and your alway obtain a zero as the result).
At the time you assign a double to an integer you lose information since you have to "throw away" anything to the right of the decimal point.

The compiler needs to be certain that you know what you are doing so it provides you an error to make confirm. You can do it, however you have to cast it first to avoid the error:

``amount = (int) (horsePower * 0.746 * rate * numberHours);``

However as has been mentioned, in case you instantly overwrite the value with the user input it's pretty irrelevant!

``````amount = horsePower*0.746*rate*numberHours;
amount = sc.nextInt();  // <---- Move or remove this!
System.out.println ("amount to pay is =" + amount);``````

## Related questions

20 views
How can I solve this error?
360 views
Problem : I am very new to Java. I am always facing the problem if I try to convert kilometers into miles. I am kind of novice programmer so please bear with me. Please find below my Java code for your reference: import java.util.Scanner; public class myProblem{    ... three days but still cannot fix it so I am looking for Java expert who can suggest me the quick fix for my above Java error.
368 views
Problem: I am very new to Java and trying to learn arrays in Java. I am trying to enter one int and the other long ex: 1 and 1000000000 and now I am trying to create the array of size 1000000000. And after creating the array at each index of my array willing to store int val, ex: arr[ ... [1]);     long []arr;        arr = new long[y+1];         for(long j=0;j<=y;j++)         arr[j] = 4;     }
1 vote
107 views
Problem: Hello guys, I find out this forum a few days ago. You guys are superb and friendly with your help. I loved the forum very much, this is really resourceful. Well, I introduce myself as a noob to you guys. Actually, I am trying to adopt the java programming ... me? I know I am doing something very fishy. Please, figure it out and let me know. I must recognize your effort and help. Thanks.
141 views
Problem: I am a na&iuml;ve to programming in C language. Please find below some relevant part of my code. I do not see any issues in my below code but still I keep on receiving below error: &ldquo;Invalid operands to binary expression ('double' and 'double').&rdquo; Please find ... is a double???? } printf ("%d\n", rem);          I need the modulo at any condition  but it is not working return 0;
99 views
I start programming recently and I love to do programming but every day I'm facing new problems. I wrote a java code where I can easily take two int input from a user and I'm able to print the smallest value. But I'm doing all things in the main method. I don't want to do ... smallest; if(a>b){ smallest = b; }else{ smallest = a; } System.out.println("Smallest is: "+smallest); } Here is my code.
7 views
Problem: I have learned to convert string to integers. Now, I want to know whether I can convert string to double in C#? If yes, how? Please elaborate with example.