0

I am trying extract the coefficients and exponents from a string array. for example: "x^2 + 2x + 1" or "x^5" or "x^2 -x + 1". The code I wrote in this Polynomial constructor works for most cases but when the length of the string is one I am getting a index out of bounds error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1". I am not sure how to fix this. Can anyone help me with this code?

I've tried to break the while loop if the length of the array is 1.

public class Polynomial extends PolynomialInterface {
    public Polynomial(String s) {
        // complete this code
        Term newTerm;
        DList<Term> dList = new DList<Term>();

        int exp = 0;
        double coef = 0;

        s = s.replaceAll(" ", "");



        String[] array = s.split("\\+|(?=-)");


       for(String i : array){
            System.out.println("split: " + i);
        }


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

                while (array[i].contains("x")) {

                    exp = 0;

                    if ((array[i].substring(0, 1).equalsIgnoreCase("x"))) {
                        coef = 1;
                    }


                    if (array[i].substring(0, 1).equals("-") && array[i].substring(1, 2).equalsIgnoreCase("x")) {
                        coef = -1;
                        exp = 1;
                    }


                    if (array[i].contains("^")) {

                        int index = array[i].indexOf("^");
                        String myString = "";

                        if (index != -1) {
                            myString = array[i].substring(index + 1, index + 2);
                            if (!myString.isEmpty()) {
                                exp = Integer.parseInt(myString);
                            }
                        }

                    }

                    if (!array[i].contains("^")) {
                        exp = 1;
                    }




                    int end = array[i].indexOf("x");

                    String subString = "";

                    if (end != -1) {
                        subString = array[i].substring(0, end);
                        if (!subString.isEmpty() && !subString.contains("-")) {
                            coef = Double.parseDouble(subString);
                        } else if (!subString.isEmpty() && !subString.equals("-")) {
                            coef = Double.parseDouble(subString);

                        }
                    }


                    newTerm = new Term(coef, exp);
                    dList.addLast(newTerm);

                    if(array.length==1){
                        break;
                    }
                    i++;

                }

                while (array[i].matches("([-]\\d*|\\d*)") && (i < array.length)) {


                    coef = Double.parseDouble(array[i]);
                    exp = 0;

                    newTerm = new Term(coef, exp);
                    dList.addLast(newTerm);


                    break;

                }
            }








        //super.data = dList;



        }

When I debugged I got the right coefficients and exponents then it iterates and I get a index out of bounds error

  • `for (int i = 0; i <= array.length; i++)` ... you do understand that Java has zero-based indexing, right? That means that the last element is at `array.length - 1` not at `array.length`. – Maarten Bodewes Oct 21 '19 at 21:14

1 Answers1

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

Your for loop goes to array.length, what causes the null pointer exception. Remember in java arrays start at 0 and end at array.length-1, so your contition should be i < array.length

NotYou
  • 31
  • 2