• Register
0 votes
1.3k 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.

3.9k points

Please log in or register to answer this question.

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

or

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

 

Hope this solution solves your problem.

38.6k points
0 votes

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);

 

31.7k points
edited by

Related questions

0 votes
0 answers 3 views
3 views
Problem: Java possible lossy conversion from double to int.
asked Jun 30 Shima 126k points
0 votes
1 answer 27 views
27 views
Problem: I have no idea to solve this, somebody help : Error: incompatible types: possible lossy conversion from double to int
asked Jun 17 asha 121k points
0 votes
1 answer 1 view
1 view
Problem: Lossy conversion from double to int?
asked 1 day ago Nahil 128k points
0 votes
1 answer 94 views
94 views
How can I solve this error?
asked Dec 19, 2020 TeamScript 13.5k points
2 votes
1 answer 852 views
852 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.
asked May 7, 2020 stewart 4k points
0 votes
1 answer 4 views
4 views
Problem: Please,I want an answer : Incompatible types: possible lossy conversion from int to char
asked Jun 22 Sawyer Parviz 168k points
0 votes
1 answer 1.1k views
1.1k 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;     }
asked Jul 29, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 9 views
9 views
Problem : How can I do C++ cast double to int? Need help.
asked May 28 Siam55 110k points
0 votes
1 answer 12 views
12 views
Problem : How to cast double to int c++? Please help me with this.
asked May 28 Sifat55 108k points
0 votes
1 answer 19 views
19 views
Problem : How to cast a double to int in c++? Please assist me.
asked May 28 Sifat55 108k points