ClassCastException, casting Integer to Double
Asked Answered
B

10

54
ArrayList marks = new ArrayList();
Double sum = 0.0;
sum = ((Double)marks.get(i));

Everytime I try to run my program, I get a ClassCastException that states: java.lang.Integer cannot be cast to java.lang.Double

Broad answered 6/4, 2011 at 12:27 Comment(0)
V
57

We can cast an int to a double but we can't do the same with the wrapper classes Integer and Double:

 int     a = 1;
 Integer b = 1;   // inboxing, requires Java 1.5+

 double  c = (double) a;   // OK
 Double  d = (Double) b;   // No way.

This shows the compile time error that corresponds to your runtime exception.

Vaticinal answered 6/4, 2011 at 12:55 Comment(2)
Hmm it is an old question. Java 7 lets me cast Integer to double and the magic happens just how I expected: Double km = (double) (metres *1_000);Amoebic
Just wondering why it won't be possible to convert Integer to Double? I have a piece of code that used to work, then suddenly stopped working and started throwing the same cast exception for some use-cases. An example of this failing is when the Integer value is 825842.Diachronic
S
37

Well the code you've shown doesn't actually include adding any Integers to the ArrayList - but if you do know that you've got integers, you can use:

sum = (double) ((Integer) marks.get(i)).intValue();

That will convert it to an int, which can then be converted to double. You can't just cast directly between the boxed classes.

Note that if you can possibly use generics for your ArrayList, your code will be clearer.

Steno answered 6/4, 2011 at 12:29 Comment(1)
If you use Integer.doubleValue(), there is no need to cast int to double. sum = ((Integer) marks.get(i)).doubleValue(); will be more succinct.Holbrook
F
12

The code posted in the question is obviously not a a complete example (it's not adding anything to the arraylist, it's not defining i anywhere).

First as others have said you need to understand the difference between primitive types and the class types that box them. E.g. Integer boxes int, Double boxes double, Long boxes long and so-on. Java automatically boxes and unboxes in various scenarios (it used to be you had to box and unbox manually with library calls but that was deemed an ugly PITA).

http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

You can mostly cast from one primitive type to another (the exception being boolean) but you can't do the same for boxed types. To convert one boxed type to another is a bit more complex. Especially if you don't know the box type in advance. Usually it will involve converting via one or more primitive types.

So the answer to your question depends on what is in your arraylist, if it's just objects of type Integer you can do.

sum = ((double)(int)marks.get(i));

The cast to int will behind the scenes first cast the result of marks.get to Integer, then it will unbox that integer. We then use another cast to convert the primitive int to a primitive double. Finally the result will be autoboxed back into a Double when it is assigned to the sum variable. (asside, it would probablly make more sense for sum to be of type double rather than Double in most cases).

If your arraylist contains a mixture of types but they all implement the Number interface (Integer, Short, Long, Float and Double all do but Character and Boolean do not) then you can do.

sum = ((Number)marks.get(i)).doubleValue();

If there are other types in the mix too then you might need to consider using the instanceof operator to identify them and take appropriate action.

Fourteen answered 6/1, 2015 at 18:41 Comment(0)
T
8

2 things to understand here -

1) If you are casting Primitive interger to Primitive double . It works. e.g. It works fine.

int pri=12; System.out.println((double)pri);

2) if you try to Cast Integer object to Double object or vice - versa , It fails.

Integer a = 1; Double b = (double) a; // WRONG. Fails with class cast excptn

Solution -

Soln 1) Integer i = 1; Double b = new Double(i);
soln 2) Double d = 2.0; Integer x = d.intValue();
Testa answered 9/7, 2017 at 13:41 Comment(0)
M
5

specify your marks:

List<Double> marks = new ArrayList<Double>();

This is called generics.

Mastrianni answered 6/4, 2011 at 12:29 Comment(2)
If the ArrayList was full of Doubles then he would not get a ClassCastException. The array must be full of integers, right?Tansey
@Tansey - one Integer is enough ;)Vaticinal
S
3

Changing an integer to a double

int abc=12; //setting up integer "abc"

System.out.println((double)abc); 

The code will output integer "abc" as a double, which means that it will display as "12.0". Notice how there is a decimal place, indicating that this precision digit has been stored.

Same with double if you want to change it back,

double number=13.94;

System.out.println((int)number); 

This code will print on one line, "number" as an integer. The output will be "13". Notice that the value has not been rounded up, the data has actually been omitted.

Snipes answered 12/9, 2013 at 15:51 Comment(0)
W
3
sum = Double.parseDouble(""+marks.get(i));
Wilburn answered 23/6, 2015 at 14:58 Comment(1)
Please consider adding an explanation on why and how your code snippet does answer the OP question.Spitter
B
1
Integer x=10;
Double y = x.doubleValue();
Bloemfontein answered 20/2, 2019 at 10:8 Comment(3)
Please add some explanation to that code. How does it solve the problem?Drift
It does convert x from Integer to DoublePahl
Integer wrapper class has doubleValue() method which return a double.Bloemfontein
F
0

This means that your ArrayList has integers in some elements. The casting should work unless there's an integer in one of your elements.

One way to make sure that your arraylist has no integers is by declaring it as a Doubles array.

    ArrayList<Double> marks = new ArrayList<Double>();
Fellah answered 29/4, 2013 at 18:56 Comment(0)
G
0

I think the main problem is that you are casting using wrapper class, seems that they are incompatible types.

But another issue is that "i" is an int so you are casting the final result and you should cast i as well. Also try using the keyword "double" to cast and not "Double" wrapper class.

You can check here:

Hope this helps. I found the thread useful but I think this helps further clarify it.

Generate answered 25/1, 2017 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.