CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Need HElp Now, DUE Today (http://www.codingforums.com/showthread.php?t=221961)

klagartin 03-21-2011 06:56 PM

Need HElp Now, DUE Today
 
Okay, So I was assigned a project for school.
It is to prompt the user to enter a fixed number of points, declare the max and min X values, then plug x into an equation for y and print it. I've been able to successfully do that.

Here my problem: I get this exact error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at locZero.main(locZero.java:41)

This comes into play at line 41 when im trying to use if statements and another array to record where the y values change signs from pos to neg/vise versa.

Here is the code
Code:

import java.util.Scanner;

public class locZero{
        public static void main(String[]args){
               
                Scanner input= new Scanner(System.in);
               
                double minX, maxX;
                int numPoints;
                int numChange=0;
               
                System.out.print("Enter number of points: ");
                numPoints=input.nextInt();
                System.out.print("Enter min x: ");
                minX=input.nextDouble();
                System.out.print("Enter max x: ");
                maxX=input.nextDouble();
               
               
                double [] x= new double[numPoints];
                double [] y= new double[numPoints];
                double [] change= new double[numPoints];
               
                System.out.println("i"+"\t"+"x[i]"+"\t"+"y[i]");
               
                for(int i=0;i<numPoints;i++){
                        System.out.print(i+"\t");
                       
                        x[0]=minX;
                        x[numPoints-1]= maxX;
                        x[i]=x[0]+i*((maxX-minX)/(numPoints-1));                       
                        y[i]=(((x[i])*(x[i])*(x[i]))-(2*((x[i])*(x[i])))-(5*x[i])+4);
                       
               
                        System.out.print(x[i]);
                        System.out.print("\t");
                        System.out.print(y[i]);
                        System.out.println();
                       
                        for(i=0;i<=numPoints;i++){
                                if(y[i]>=0 && y[i+1]<0){
                                        change[numChange]=i;
                                        numChange++;
                                }
                                else if(y[i]>0 && y[i+1]<=0){
                                        change[numChange]=i;
                                        numChange++;
                                }
               
                        }
                }
       
               
        }
               
}

please try to help me. I know the error means im using an array that does exist, but i dunno how i am.

Old Pedant 03-21-2011 07:58 PM

Code:

                        for(i=0;i<=numPoints;i++){
                                if(y[i]>=0 && y[i+1]<0){
                                        change[numChange]=i;
                                        numChange++;
                                }
                                else if(y[i]>0 && y[i+1]<=0){

Your other loops only went to i < numPoints. With this loop, you are guaranteed to be 1 past the end of the array.

But then you make it worse:
How can you use i+1 if i is already at or past the maximum value for the array subscript??


All times are GMT +1. The time now is 03:32 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.