Can you see why I am getting an ExceptionOutOfBoundsError?

Hey guys, this is my code. I know it's long, but I decided to put it all there in case you would need it.
The problem is an out of bounds exception that I can't get rid of.
Its on the line where finalCoeff[k]=coeffArray[i]*coeffArray2[j]; is.

The point of it is to take in two polynomials and then multiply them. I can get them in, but multiplying them takes a lot more effort.

thanks you in advance


import java.util.Scanner;
import java.util.*;

public class SecondMain
{

//Default Constructor
public SecondMain(){}

public static void main(String[]args)
{
Scanner key=new Scanner(System.in);
int numberOfTerms, numberOfTerms2;
int i=0, j,menuChoice;
int example, temp;

//Creation of Polynomial object p
Polynomial p=new Polynomial();

do{
//First polynomial
System.out.print("Let's start with the first polynomial.
"+"How many TERMS does it have? ");
numberOfTerms=key.nextInt();

int coeffArray[]=new int[numberOfTerms];
int powerArray[]=new int[numberOfTerms];


for(i=0;i<numberOfTerms;i++)// loop that asks for the coefficients of each term
{
System.out.print("Okay, what is the COEFFICIENT of term "+(i+1)+"?(You must indicate if it is negative)
");
coeffArray[i]=key.nextInt();
}

for(i=0;i<numberOfTerms;i++)// loop that asks for the powers of each term
{
System.out.print("Alright, what is the POWER of term "+(i+1)+"?(You must indicate if it is negative)
");
powerArray[i]=key.nextInt();
}

System.out.print("So your polynomial would be: ");

for(i=0;i<numberOfTerms;i++)
{
System.out.print(coeffArray[i]);
System.out.print("x^");
System.out.print(powerArray[i]+"
");
}

//Second polynomial
System.out.print("Now, let's do the SECOND polynomial.
"+"How many TERMS does it have? ");
numberOfTerms2=key.nextInt();

/*if(numberOfTerms2!= 0||1||2||3||4||5||6||7||8||9)
{System.out.print("Sorry, I was not able to read your answer, please start again.");
}*/

int coeffArray2[]=new int[numberOfTerms2];
int powerArray2[]=new int[numberOfTerms2];


for(i=0;i<numberOfTerms2;i++)// loop that asks for the coefficients of each term
{
System.out.print("Okay, what is the COEFFICIENT of term "+(i+1)+"?(You must indicate if it is negative)
");
coeffArray2[i]=key.nextInt();
}

for(i=0;i<numberOfTerms2;i++)// loop that asks for the powers of each term
{
System.out.print("Alright, what is the POWER of term "+(i+1)+"?(You must indicate if it is negative)
");
powerArray2[i]=key.nextInt();
}


System.out.print("So your polynomial would be: ");

for(i=0;i<numberOfTerms2;i++)
{
System.out.print(coeffArray2[i]);
System.out.print("x^");
System.out.print(powerArray2[i]+"
");
}

System.out.print("Would you like to: 1. Multiply them, or, 2. reset them?");
menuChoice=key.nextInt();
//Switch case
switch(menuChoice)
{

case 1: p.computePolynomials(coeffArray,coeffArray2,powerArray, powerArray2, numberOfTerms, numberOfTerms2);

case 2: break;

}
} while(menuChoice==2);

}
}

public class Polynomial
{
int i, j, k=0, max;
int finalCoeff[]=new int[25];
int finalPower[]=new int[25];

public Polynomial()
{
}

public void computePolynomials(int coeffArray[],int coeffArray2[],int powerArray[], int powerArray2[], int numberOfTerms, int numberOfTerms2)
{
max=(numberOfTerms*numberOfTerms2);//This is the number of iterations that the following loop will go through.

for(i=0;i<max;i++)//Loop that goes through all of the polynomials and prints them
{
for(j=0;j<coeffArray2.length;j++)
{

finalCoeff[k]=coeffArray[i]*coeffArray2[j];
k++;

}
}
finalPower[i]=powerArray[i]+powerArray2[i];
System.out.print(finalCoeff[i]);
System.out.print("x^");
System.out.print(finalPower[i]);
}


}

Comments

  • Hey,
    A debugging statement was added to show array element pointer values during looping. Logic modified for looping requirement - if this is not valid then mayby another variable is required for max looping but "i" can not be that variable. After looping index "i" is too high so it is adjusted to point to last element. Logic may still need tweeking.
    [code]
    import java.util.Scanner;
    import java.util.*;

    public class SecondMain {

    //Default Constructor
    public SecondMain() {
    }

    public static void main(String[] args) {
    Scanner key = new Scanner(System.in);
    int numberOfTerms, numberOfTerms2;
    int i = 0, j, menuChoice;
    int example, temp;

    //Creation of Polynomial object p
    Polynomial p = new Polynomial();

    do {
    //First polynomial
    System.out.print("Let's start with the first polynomial.
    " + "How many TERMS does it have? ");
    numberOfTerms = key.nextInt();

    int coeffArray[] = new int[numberOfTerms];
    int powerArray[] = new int[numberOfTerms];


    for (i = 0; i < numberOfTerms; i++)// loop that asks for the coefficients of each term
    {
    System.out.print("Okay, what is the COEFFICIENT of term " + (i + 1) + "?(You must indicate if it is negative)
    ");
    coeffArray[i] = key.nextInt();
    }

    for (i = 0; i < numberOfTerms; i++)// loop that asks for the powers of each term
    {
    System.out.print("Alright, what is the POWER of term " + (i + 1) + "?(You must indicate if it is negative)
    ");
    powerArray[i] = key.nextInt();
    }

    System.out.print("So your polynomial would be: ");

    for (i = 0; i < numberOfTerms; i++) {
    System.out.print(coeffArray[i]);
    System.out.print("x^");
    System.out.print(powerArray[i] + "
    ");
    }

    //Second polynomial
    System.out.print("Now, let's do the SECOND polynomial.
    " + "How many TERMS does it have? ");
    numberOfTerms2 = key.nextInt();

    /*if(numberOfTerms2!= 0||1||2||3||4||5||6||7||8||9)
    {System.out.print("Sorry, I was not able to read your answer, please start again.");
    }*/

    int coeffArray2[] = new int[numberOfTerms2];
    int powerArray2[] = new int[numberOfTerms2];


    for (i = 0; i < numberOfTerms2; i++)// loop that asks for the coefficients of each term
    {
    System.out.print("Okay, what is the COEFFICIENT of term " + (i + 1) + "?(You must indicate if it is negative)
    ");
    coeffArray2[i] = key.nextInt();
    }

    for (i = 0; i < numberOfTerms2; i++)// loop that asks for the powers of each term
    {
    System.out.print("Alright, what is the POWER of term " + (i + 1) + "?(You must indicate if it is negative)
    ");
    powerArray2[i] = key.nextInt();
    }


    System.out.print("So your polynomial would be: ");

    for (i = 0; i < numberOfTerms2; i++) {
    System.out.print(coeffArray2[i]);
    System.out.print("x^");
    System.out.print(powerArray2[i] + "
    ");
    }

    System.out.print("Would you like to: 1. Multiply them, or, 2. reset them?");
    menuChoice = key.nextInt();
    //Switch case
    switch (menuChoice) {

    case 1:
    p.computePolynomials(coeffArray, coeffArray2, powerArray, powerArray2, numberOfTerms, numberOfTerms2);

    case 2:
    break;

    }
    } while (menuChoice == 2);

    }
    }

    class Polynomial {

    int i, j, k = 0, max;
    int finalCoeff[] = new int[25];
    int finalPower[] = new int[25];

    public Polynomial() {
    }

    public void computePolynomials(int coeffArray[], int coeffArray2[], int powerArray[], int powerArray2[], int numberOfTerms, int numberOfTerms2) {
    max = (numberOfTerms * numberOfTerms2);//This is the number of iterations that the following loop will go through.

    // for (i = 0; i < max; i++)//Loop that goes through all of the polynomials and prints them
    // NOTE CHANGE in loop constraint
    for (i = 0; i < [color=Orange]coeffArray.length[/color]; i++)//Loop that goes through all of the polynomials and prints them
    {
    for (j = 0; j < coeffArray2.length; j++) {
    [color=Orange]System.out.printf("k =%d,i=%d,j=%d
    ", k, i, j);[/color] // display array pointers
    finalCoeff[k] = coeffArray[i] * coeffArray2[j];
    k++;

    }
    }
    [color=Orange]i = i - 1; // NOTE CHANGE NEW Statement[/color]
    finalPower[i] = powerArray[i] + powerArray2[i];
    System.out.print(finalCoeff[i]);
    System.out.print("x^");
    System.out.print(finalPower[i]);
    }
    }
    [/code]
    regards, se52
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion