Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need Help! Out of bounds error on ARRAYS!!

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry Need Help! Out of bounds error on ARRAYS!!

    My Task:
    ---Example: 2 2 3 20 10 7 9 6 8 8 8 2 9 12 14 5 5 5 5 19
    plateau = 6 8 8 8 2 because 8 is the longest sequence of identical
    numbers where the numbers to the left and right are lower.



    MY ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 500
    at Lab8.main(Lab8.java:60)




    int [] arr3 = new int [500];
    for(int i = 0; i < arr3.length;i++){
    	int y = gen.nextInt(20)+1;
    	arr3[i] = y;
    	System.out.print(" " + y);
    }
    		int tempBookend1 = 0;
    		int tempBookend2 = 0;
    		int tempPos1 = 0;
    		int tempPos2 = 0;
    		int tempLength = 0;
    		int tempType = 0;
    		int bookend1 = 0;
    		int bookend2 = 0;
    		int pos1 = 0;
    		int pos2 = 0;
    		int length = 0;
    		int type = 0;
    		for(int i = 0; i < arr3.length;i++){
    			for(int j = 1; j <= arr3.length-2;j++){
    				if( tempBookend1 < arr3[tempPos1] && tempBookend2 < arr3[tempPos2] && tempLength > length){
    					bookend1 = tempBookend1;
    					bookend2 = tempBookend2;
    					pos1 = tempPos1;
    					pos2 = tempPos2;
    					length = tempLength;
    					type = tempType;
    				}
    				else if(arr3[i] == arr3[i+1]){ // I get an error here
    					tempPos1 = i;
    					tempBookend1 = arr3[i-1];
    				}
    				else if(arr3[i] != arr3[i+1]){  // I get an error here
    					tempPos2 = i; 
    					tempBookend2 = arr3[i+1];
    					tempType = arr3[i];
    					tempLength = (tempPos2 - tempPos1);
    				}
    			}
    		}


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need Help! Out of bounds error on ARRAYS!!

    The outer for loop indicates that i can grow as large as arr3.length-1. But, on the two lines you indicate as troublesome you use the expression arr3[i+1] which is past the end of the array and, hence, liable to cause an ArrayIndexOutOfBoundsException.

    You should rethink the logic of the loops so that you don't attempt to access the array past its end (ie past index arr3.length-1).

    Doing this will involve reading the question closely to determine whether a run of large numbers right at the end of the array counts as a plateau or not.

Similar Threads

  1. Out of bounds error, 2d irregular array of integers
    By Farmer in forum Loops & Control Statements
    Replies: 3
    Last Post: August 1st, 2011, 03:55 PM
  2. error related arrays outputting wront data...
    By semicolon in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 1st, 2011, 09:34 AM
  3. problems getting past the out of bounds error
    By dbdny in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 05:57 PM
  4. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM

Tags for this Thread