if i'm not mistaken, "0.5" are decimal numbers; thus, making it a float value. but why is java telling me that it is a double?. the return statements are detected as errors by java, saying: "incompatible types:possible lossy conversion from double to float"
public float typeDmgMultiplr(String type,String type2){
if(type.equalsIgnoreCase("grass")){
if(type2.equalsIgnoreCase("grass"))
return 0.5;
else if(type2.equalsIgnoreCase("poison"))
return 0.5;
else if(type2.equalsIgnoreCase("fire"))
return 0.5;
else
return 2.0;
}
else if(type.equalsIgnoreCase("fire")){
if(type2.equalsIgnoreCase("grass"))
return 2.0;
else if(type2.equalsIgnoreCase("poison"))
return 1.0;
else if(type2.equalsIgnoreCase("fire"))
return 0.5;
else
return 0.5;
}
else if(type.equalsIgnoreCase("water")){
if(type2.equalsIgnoreCase("grass"))
return 0.5;
else if(type2.equalsIgnoreCase("poison"))
return 1.0;
else if(type2.equalsIgnoreCase("fire"))
return 2.0;
else
return 0.5;
}
else{
if(type2.equalsIgnoreCase("grass"))
return 2.0;
else if(type2.equalsIgnoreCase("poison"))
return 0.5;
else if(type2.equalsIgnoreCase("fire"))
return 1.0;
else
return 1.0;
}
}
As float can have decimal values that don't have corresponding long value. Therefore, we'll receive the same error. The double values can be too large or too small for an int and decimal values will get lost in the conversion. Hence, it is a potential lossy conversion.
Here double is a primitive data type its object can't be used to call the Float class method.
Solution: 1) be consistent, and 2) use names starting with lowercase letters for method names ... as per the Java style guides. The second problem is due to the method signature for Math. pow ; see the javadoc. It returns the result as a double .
If I'm not mistaken,
0.5
are decimal numbers; thus, making it a float value.
You should not rely solely on your intuition when learning a new programming language.
In fact, 0.5
is a double
literal. For a float
literal you need to write 0.5f
.
As The Java Language Specification (JLS 3.10.2) states:
A floating-point literal is of type
float
if it is suffixed with an ASCII letterF
orf
; otherwise its type isdouble
and it can optionally be suffixed with an ASCII letterD
ord
.
See also:
You have two options.
One is change your method's return type to double.
The other is change the double values you are returning to float values, as many have said in the comments.
public float typeDmgMultiplr(String type,String type2){
if(type.equalsIgnoreCase("grass")){
if(type2.equalsIgnoreCase("grass"))
return 0.5f;
else if(type2.equalsIgnoreCase("poison"))
return 0.5f;
else if(type2.equalsIgnoreCase("fire"))
return 0.5f;
else
return 2.0f;
}
else if(type.equalsIgnoreCase("fire")){
if(type2.equalsIgnoreCase("grass"))
return 2.0f;
else if(type2.equalsIgnoreCase("poison"))
return 1.0f;
else if(type2.equalsIgnoreCase("fire"))
return 0.5f;
else
return 0.5f;
}
else if(type.equalsIgnoreCase("water")){
if(type2.equalsIgnoreCase("grass"))
return 0.5f;
else if(type2.equalsIgnoreCase("poison"))
return 1.0f;
else if(type2.equalsIgnoreCase("fire"))
return 2.0f;
else
return 0.5f;
}
else{
if(type2.equalsIgnoreCase("grass"))
return 2.0f;
else if(type2.equalsIgnoreCase("poison"))
return 0.5f;
else if(type2.equalsIgnoreCase("fire"))
return 1.0f;
else
return 1.0f;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With