Hello. I have a question. I have a polynomial with Double coefficients and one with Integer ones, and to perform the integration I have to convert from object Integer to Double,but I found that it is not possible so I've tried to convert it into a String than into Double,but i get some exceptions.Do you have any idea on other solution for conversion or why am i getting this exceptions?:)
Thanks

``````Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at Poly.IntegerPolynomial.getCoeff(IntegerPolynomial.java:42)
at Poly.IntegerPolynomial.getCoeff(IntegerPolynomial.java:1)
at Poly.Polynomial.integrate(Polynomial.java:230)
at Poly.Gui.actionPerformed(Gui.java:517)

public RealPolynomial integrate (Polynomial p)
{
Double [] coefficients=new Double[p.getCoeff().length+1];
Double [] finalCoefficients=new Double[p.getCoeff().length+1];
Double [] coeff=new Double[p.getCoeff().length+1];
String s;

for(int v=0;v<coeff.length;v++)
{s=p.getCoeff(v).toString();
System.out.println(s);
coeff[v]=Double.valueOf(s);
}

RealPolynomial result;
//set the coefficients according to the integration rule,we obtain real coefficients
for (int i=1;i<coefficients.length;i++)
coefficients[i]=coeff[i-1]/Double.valueOf(i);

for(int i=0;i<coefficients.length;i++)
{finalCoefficients[i]=coefficients[i];
System.out.println(coefficients[i]);
}

result=new RealPolynomial(coeff,coeff.length-1);

return result;
}
``````

Unless you are runnning a very old verison of Java you can convert an Integer to a Double trivially using auto boxing/unboxing:

`````` Integer i = 3;
Double d = new Double(i);
``````

Your array index exception is at line 42 of Poly.IntegerPolynomial.getCoeff, where you are trying to access element number 3 of an array that doesn't have 4 elements (see the exception message), but since you didn't post that code it's hard to say any more.

You can also convert integer to double by using the below statements

``````    Integer i = 23;
Double num = (Double)i;
``````

You can also convert integer to double by using the below statements..

Not in Java you can't!

k99rs
Welcome to DaniWeb.
Next time you post you may want to check any code you post, because otherwize you may end up looking foolish.The code you posted does not even compile ("Cannot cast from Integer to Double")

Thanks,I tried your way but I still got some Exceptions , but I figured out another way,

`````` Double i;
Integer number;
i=(double)(number.intValue())
``````

and it works!:)

That's odd! Which way did you try? What execption(s) did you get? The code I posted certainlyshould compile/run on JAva 1.5 or later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.