Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Need HElp Now, DUE Today

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-21-2011, 07:56 PM PM User | #1 klagartin New to the CF scene   Join Date: Mar 2011 Posts: 3 Thanks: 1 Thanked 0 Times in 0 Posts 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=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.
 03-21-2011, 08:58 PM PM User | #2 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,949 Thanks: 75 Thanked 4,307 Times in 4,274 Posts 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?? __________________ An optimist sees the glass as half full. A pessimist sees the glass as half empty. A realist drinks it no matter how much there is.
 Users who have thanked Old Pedant for this post: Fot (03-23-2011)

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 07:51 PM.