# Possible lossy conversion from double to int

1 vote
136 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 language with the help of internet and many voluntary websites. I am trying to write a program that will add, subtract, multiply, divide two numbers sequentially over the line, but it’s not happening for some reason. I am attaching my sample code here

``````public class MyClass{
public static void main(String []args){
float num1 = 12.10f;
int num2 = 10;
int sum, sub, mul, div;
sum = num1+num2;
sub = num1-num2;
mul = num1*num2;
div = num1/num2;
System.out.println("Sum value is: " + sum);
System.out.println("Sub value is: " + sub);
System.out.println("Mul value is: " + mul);
System.out.println("Div value is: " + div);
}
}``````

The above program is delivering an output

``````MyClass.java:7: error: incompatible types: possible lossy conversion from float to int
sum = num1+num2;
^
MyClass.java:8: error: incompatible types: possible lossy conversion from float to int
sub = num1-num2;
^
MyClass.java:9: error: incompatible types: possible lossy conversion from float to int
mul = num1*num2;
^
MyClass.java:10: error: incompatible types: possible lossy conversion from float to int
div = num1/num2;``````

What could be a solution for 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.

1 vote

## Solution:

Let me give you a big thanks for your words. We are trying to build a network for the people who are learning programming languages.

However, possible lossy conversion from float to int is an incompatible error produced by the java compiler. It means you are trying to print some values from one type to another type without casting them. A compiler can’t allow you to this. Before printing the value, you must have to cast the value in the same type.

In your case, you declared two variables one is float type and the other one is an integer type. Later on, you tried to print the value of an integer type. That’s absurd! Sp cast the float into an integer before printing them (assuming you want to print them as integers).

``````public class MyClass{
public static void main(String []args){
float num1 = 12.10f;
int num2 = 10;
int sum, sub, mul, div;
sum = (int) num1+num2;
sub = (int) num1-num2;
mul = (int) num1*num2;
div = (int) num1/num2;
System.out.println("Sum value is: " + sum);
System.out.println("Sub value is: " + sub);
System.out.println("Mul value is: " + mul);
System.out.println("Div value is: " + div);
}
}``````

Now, the program looks okay and it should print the values as follows

``````Sum value is: 22
Sub value is: 2
Mul value is: 120
Div value is: 1``````

## Related questions

55 views
How can I solve this error?
1K 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 ... unable to understand the above error saying cannot find symbol. Please help me I am really been stuck on this error for a while.
592 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.
13 views
Problem: Can anyone tell me the solution of this problem? possible lossy conversion from int to char
697 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;     }
17 views
How can I solve this problem?
4 views
Problem: I'm not sure it's me or what, but I'm having a problem converting a double string. here is my code: double total = 44; String total2 = Double.toString(total); Am I doing something wrong or am I missing a step from here. I have an ... in an onTouchListener. Ill post more code, basically when the user touches the edittext box I want the information to calculate to fill the edittext box.