Reputation: 321
I am trying to compute few calculations for my project and I get following java.lang.ClassCastException
for
y2= (double) molWt[x];
molWt
and dist
are two array of type Number and double
respectively.
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
public Double calcData(Number[] molWt, Double[] dist, String unk) {
Double y2,y1, newY = null;
Double x1,x2;
Double unkn = Double.parseDouble(unk.toString());
Double prev=0d;
Double slope;
for (int x = 0; x < dist.length; x++)
if (unkn > prev && unkn < dist[x]) {
y2 = (double) molWt[x];
y1 = (double) molWt[x - 1];
x2 = dist[x];
x1 = dist[x - 1];
slope = ((y2 - y1) / (x2 - x1));
newY = slope * (unkn - x1) + y1;
} else {
prev = dist[x];
}
return newY;
}
Upvotes: 15
Views: 28535
Reputation: 101
you can use java.lang.Number.doubleValue() method to cast a number to a double object.
y2= molWt[x].doubleValue()
Upvotes: 2
Reputation: 3502
Use Number.doubleValue():
y2 = molWt[x].doubleValue();
instead of trying to cast. Number
cannot be cast to a primitive double
.
Upvotes: 33