Why is it saying incompatible types ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is it saying incompatible types ?

When the house variable is an int type, the double variable isn't showing any decimal values. Like if the house variable is 3 and an int type and if I do "double A = 100 / house; it is showing 33.0 but the decimal values are not showing, it should be 33.3333.... something with decimal values. When I change the house variable to a double type, its working fine but now I can't divide house with 100, for example if I do - int D = 100 / house; - its showing incompatible types as house is now a double type variable, I need both answer where double A = 100 / house; will have decimals and int D = 100 / house; will not have decimals... the code - class MyClass { public static void main(String[] args) { int houses = 3; double B = 100 / houses; System.out.println(B); int C = 100 / houses; System.out.println(C); } } Any solutions guys ?

22nd Jul 2020, 4:42 AM
Shouvik
Shouvik - avatar
2 Answers
+ 1
codemonkey thanks a lot it works !!! I didn't know about that "type cast for B" - what does it mean and how does that work ? Is it mentioned in any java lessons in sololearn ?
22nd Jul 2020, 8:01 AM
Shouvik
Shouvik - avatar
+ 1
codemonkey Okk, I will search about this type cast in yt or google
22nd Jul 2020, 8:18 AM
Shouvik
Shouvik - avatar