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 6 of 6

Thread: Index out of bounds exception. Need help.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Index out of bounds exception. Need help.

    Hey all. I am struggling with this exercise from my book. My problem is that I can't even run the program, because it gives me
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    at domashno.ten.longestSortedSequence(ten.java:37)
    at domashno.ten.main(ten.java:17)
    public static void main(String[] args) {
    		int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
     
    		longestSortedSequence(arr);
    		System.out.println(longestSortedSequence(arr));
    		}
     
    	public static int longestSortedSequence(int[] arr) {
     
    			int beg=0;
    			int end=0;
     
    			int count=0;
     
    		for(int i=end;i<=arr.length-1;i++){
    			int begTemp=0;
    			int endTemp=0;
    			int tempCount=0;
     
    			for(int j=i+1;j<=arr.length;j++){
     
    			begTemp=arr[end];
     
    				if(arr[end]<=arr[j]){
    					tempCount++;
    					}
     
    				if(arr[end]>arr[j]){
    				endTemp=arr[j-1];
    					}
     
    			}
     
    				if(count<tempCount){
    					count=tempCount;
    					end=endTemp;
    					beg=begTemp;
    				   }
    			}
    				int length=0;
    				for(int k=arr[beg];k<=arr[end];k++){
     
    				length++;
    				}
     
    		return length;
     
    				}
    }
    Last edited by chernorizec; July 13th, 2014 at 04:50 PM. Reason: recommended by mod


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Index out of bounds exception. Need help.

    it gives me index out of bounds exception.
    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please fix the formatting of the code. The {}s are poorly placed making the code very to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Index out of bounds exception. Need help.

    I hope it's better now.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Index out of bounds exception. Need help.

    I hope it's better now.
    Did you look to see what you had done to it?

    Where is the error message?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Index out of bounds exception. Need help.

    it's on the top of the post.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Index out of bounds exception. Need help.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
    at domashno.ten.longestSortedSequence(ten.java:37)
    Look at line 37 in the code and see why it was using an index past the end of the array.
    Remember array indexes range in value from 0 to the array length-1;
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    chernorizec (July 13th, 2014)

Similar Threads

  1. Airline having with array index out of bounds exception
    By dubs4sam in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 22nd, 2014, 02:57 PM
  2. Help needed for string index out of bounds exception
    By BFF in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2013, 10:48 PM
  3. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  4. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM