• Register
0 votes
13 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,050 points

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,930 points

Related questions

0 votes
1 answer 6 views
6 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 kalyani456 280 points
1 vote
1 answer 91 views
91 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 Gavin 15.3k points
0 votes
1 answer 11 views
11 views
Problem: Hi! I want to check whether an array is empty or null in Java. Any guidance would be appreciated.
asked Oct 12 Sheeza 2.9k points
0 votes
1 answer 12 views
12 views
I want to know how to sort an array in java without using sort method.
asked Sep 28 Daniel Anderson 4k points
1 vote
1 answer 12 views
12 views
Problem: Hello Kodlogs, I have got a question from my fellow (java learner) friend and the question is: find the largest number in an array java I know about the array and I can print all the values sequentially. Moreover, I can add two different values of an array in ... Could any of you please give me a proper solution and tell me how to find the largest number in an array java? Thanks to all.
asked Mar 29 Gavin 15.3k points