What this method should do is take an array of doubles and convert them into a polynomial, for example, if the array is given was [2.0, 3.0, -2.0] the method's output would be 2.0x^2 + 3.0x^1 - 2.0. What I have done is created two loops however, when I do this both outputs are separated as expected. My output looks like this 2.0 1.0 2.0 1.0 x^3 x^2 x^1, if there is a way to have the exponents printed after the coefficient that would solve this issue.
public String printPoly(Double[] doubles) {
String polynomialString = "";
for (int i = 0; i < doubles.length; i++) {
polynomialString += doubles[i] + " ";
}
for (int j = doubles.length - 1; j >= 0; j--) {
if (j == 0) {
polynomialString += " ";
break;
}
polynomialString += "x^" + j + " ";
}
return polynomialString;
}