• Register
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.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

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

 

13 9 6
94,260 points

Related questions

0 votes
1 answer 55 views
55 views
How can I solve this error?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
2 answers 1K views
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.
asked Jan 14, 2020 jwilliam 3.9k points
2 votes
1 answer 592 views
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.
asked May 7, 2020 stewart 4k points
0 votes
1 answer 13 views
13 views
Problem: Can anyone tell me the solution of this problem? possible lossy conversion from int to char
asked Apr 7 Ifra 25.7k points
0 votes
1 answer 697 views
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;     }
asked Jul 29, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 17 views
17 views
How can I solve this problem?
asked Dec 19, 2020 TeamScript 13.5k points
0 votes
1 answer 4 views
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.
asked Apr 5 sasha 16.2k points
0 votes
1 answer 20 views
20 views
Problem: Let&rsquo;s say that the double variable x holds the value 3.6987 and needs to be converted to an int. There are two ways that this can be done: Which type of methods use for converting double type into int type? Given a Double real number, the task is to convert it into Integer in Java. Examples: Input: double = 3452.234 Output: 3452 Input: double = 98.23 Output: 98
asked Dec 3, 2020 Mashhoodch 13k points
0 votes
1 answer 31 views
31 views
I am a noob in java, I have a task to cast a double to an int in java and I wrote a program to solve this: public static void main(String args[]) { double data = 3452.345; int value = data; System.out.println(value); } But it gives an error: Exception ... \Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) What is the problem in my code?
asked Mar 15, 2020 LizzyM 6.1k points
1 vote
1 answer 311 views
311 views
Problem: I am getting an error from my very simple c++ program. It says invalid conversion from int to int* What does it actually mean? Why I am experiencing this error? I am attaching my code snippet below. Please, check the code and fix the error if you can. ... } and the output I am getting is: invalid conversion from `int*' to `int' Many thanks for your effort and valuable time, guys.
asked Apr 8, 2020 Gavin 15.3k points