Programmers learn & share
0 votes

Problem :

I am facing the below error in my program saying: error: cannot find symbol

symbol: method Cube(int)

location: class Lyrics 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.

by (3.9k points)   | 145 views

2 Answers

0 votes

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`.


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


Hope this solution solves your problem.

by (36.1k points)  
0 votes


When you convert double to int,the precision of the value is lost. For example, When you convert 4.8657 (double) to int.The int value will be 4.Primitive int does not store decimal numbers.So you will lose 0.8657.

In your case,0.7 is a double value(floating point treated as double by default unless mentioned as float-0.7f). When you calculate price*much*0.7 ,the answer is a double value and so the compiler wouldn't allow you to store it in a type integer since there could be a loss of precision.So that's what is possible lossy conversion,you may lose precision.

So what could you do about it? You need to tell the compiler that you really want to do it.You need to tell it that you know what you are doing. So explicitly convert double to int using 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 case,since 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, but the error you are getting is here:

amount = horsePower*0.746*rate*numberHours;

amount is an integer, but the result of the expression on the right is a double (because the presence of floating point data "0.746" and "rate" means 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 get a zero as the result).
When you assign a double to an integer you lose information because you have to "throw away" anything to the right of the decimal point.

The compiler wants to be sure that you know what you are doing so it gives you an error to make sure. You can do it, but you have to cast it first to avoid the error:

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

But as has been mentioned, if you immediately 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);


ago by (10k points)  
2,218 questions
2,683 answers
241 users