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?:)

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++)

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

            for(int i=0;i<coefficients.length;i++)

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

            return result;

Edited by tudor.laze: no reason

5 Years
Discussion Span
Last Post by JamesCherrill

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.

Edited by JamesCherrill


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

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

Edited by k99rs


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

Not in Java you can't!

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;

and it works!:)

Edited by tudor.laze


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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.