Results 1 to 3 of 3
  1. #1
    er1c550n20 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default ArrayIndexOutOfBoundsException

    How to remove given error
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at FindValue.main(FindValue.java:11)

    the code is:
    Java Code:
     class FindValue
     {
     	public static void main(String args[])
     	{
     		int[] array = {0,1,2,3,4,5,6,7,8,9};
     		int searchval = 7;
     		int i = 0;
     		boolean found = false;
     		for(;i<=array.length;i++)
     		{
     			if(array[i]==searchval)
     			{
     				found = true;
     			}
     		}
     		if(found)
     		{
     			System.out.println("Found" + searchval + "at index " +i);
     		}
     		else
     		{
     			System.out.println(searchval + "not in the array");
     		}
     	}
     }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 04-07-2010 at 06:22 PM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Usually when iterating through arrays with a for loop, the loop index goes from i = 0 to i < array.length, not i <= array.length. Do you see why?

    Oh, and Please read my link on code tags.

    Much luck!

  3. #3
    er1c550n20 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. How to handle ArrayIndexOutOfBoundsException
    By 3xpr1ment in forum New To Java
    Replies: 6
    Last Post: 03-03-2010, 03:16 PM
  2. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  3. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  4. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •