• Register
0 votes
18 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 in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: possible lossy conversion from double to int
	at javastackoverflow.NewClass1.main(NewClass1.java:14)
C:\Users\Hp\AppData\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?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

0 votes
You have to parse a double (data) into Int data you can code like that :
  
    public static void main(String args[]) 
    { 
  
        double data = 3452.345; 
 
        int value = (int)data; 

        System.out.println(value); 
    } 

 

11 6 4
34,950 points

Related questions

0 votes
1 answer 15 views
15 views
Problem: Let’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 6.1k points
0 votes
1 answer 3 views
3 views
How can I find an index of a certain value in a Java array?
asked 1 day ago TeamScript 10.3k points
0 votes
1 answer 3 views
3 views
Problem
asked Jan 13 alexh 2.2k points
0 votes
1 answer 7 views
7 views
Problem: I have two integers a and b. I need to calculate a/b and as outcome I would like to get float. For example as an outcome of 7/2 I would like to have 3.5. Could you please help me with that?
asked Oct 9, 2020 kalyani456 280 points
0 votes
1 answer 14 views
14 views
Problem: I need to convert a float value taken input from user to int value for calculation. How can I do that? Any suggesstions?
asked Oct 11, 2020 Code Learner 5.5k points
0 votes
1 answer 77 views
77 views
I am new to this. I just want to know how to do that? Help me.
asked Nov 7, 2020 TeamScript 10.3k points
0 votes
1 answer 18 views
18 views
How can I solve this error?
asked Dec 19, 2020 TeamScript 10.3k points
1 vote
1 answer 107 views
107 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 ... 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.
asked Apr 12, 2020 Gavin 15.3k points
0 votes
2 answers 680 views
680 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
1 vote
1 answer 205 views
205 views
Problem: I am looking for a solution to my existing code. Well, I am a new student of java. I was thinking about a program that can check whether the given char is an upper case or not! Let’s say I’ve given a char as ‘a’ and I want ... , my question is, how to check whether a char variable ch is an uppercase letter, you write? Thanks for your valuable time and effort guys.
asked Mar 22, 2020 Gavin 15.3k points