Results 1 to 5 of 5
  1. #1
    Pacina is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default [Q] Need help array

    Hello I'm new to this forum and I'm looking for a bit of help with an array that won't work for me it just gives me this error and I can't see why
    ERROR: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Mid_Exam3.main(Mid_Exam3.java:29)

    Here's the code
    Java Code:
    import java.util.Scanner;
    public class Mid_Exam3 
    {
    
    	
    	public static void main(String[] args) 
    	{
    		Scanner myinput = new Scanner(System.in);
    		
    		final int MAX_SIZE = 10;
    		int [] list = new int [MAX_SIZE];
    		int average;
    		int check;
    		int i;
    		int sum = 0;
    		int flag = 0;
    		
    		for (i=0; i<MAX_SIZE; i++)
    		{
    			System.out.println("Enter a number and press return");
    			list[i] = myinput.nextInt();
    			sum = sum + list[i];
    		}
    		
    		System.out.println();
    		
    		for (i=0; i<=MAX_SIZE; i++)
    		{
    			System.out.print(list[i]+" ");
    		}
    		
    		System.out.println();
    		
    		System.out.println("Average = "+sum/MAX_SIZE);
    		
    		System.out.println("Please enter another number and press return");
    		check = myinput.nextInt();
    		
    		for (i=0; i<MAX_SIZE; i++)
    		{
    			if (check == list[i])
    			{
    				System.out.println(check+" is on the list in postin "+i);
    				flag = flag+1;
    			}
    			else
    			{
    				System.out.println(check+" is not on the list");
    			}
    		}	
    	}
    
    }
    I can't figure why its going out of bounds cause it takes in the 10 inputs and then prints out the array and then the error
    Any help that you can give me would be great also not looking for somebody to just say change this to this if you could tell me why anything needs to be changed or where I went wrong that would be great thanks in advance hopefully I'll be able to help some other members here

  2. #2
    Pacina is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: [Q] Need help array

    Can anybody please help me with this I have an exam tomorrow and I can't figure this one out

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

    Default Re: [Q] Need help array

    Do you see any difference between these two for loops?

    Java Code:
            for (i=0; i < MAX_SIZE; i++)
            {
                // ...
            }
             
            // ...
             
            for (i=0; i <= MAX_SIZE; i++)
            {
                // ...
            }
    It's the second loop that is causing the AIOOBE to be thrown.

    Quote Originally Posted by Pacina View Post
    Can anybody please help me with this I have an exam tomorrow and I can't figure this one out
    Please don't bump your question only 1 hour after posting it. You may be in a rush, but remember that we are all volunteers, and we will try to help you as soon as we can, but please understand that your question is no more important than any other question here.

  4. #4
    Pacina is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Default Re: [Q] Need help array

    Thanks didn't see that. Didn't mean the second post to be a bump just wanted to get ans go I could make sure I understood the problem if it was a big one before tomorrow but I'll refrain from doing it again.
    Also what is AIOOBE never heard of that
    Thanks again

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

    Default Re: [Q] Need help array

    Quote Originally Posted by Pacina View Post
    Thanks didn't see that. Didn't mean the second post to be a bump just wanted to get ans go I could make sure I understood the problem if it was a big one before tomorrow but I'll refrain from doing it again.
    Thanks.

    Also what is AIOOBE never heard of that
    A bad acronym for ArrayIndexOutOfBoundsException.

    Thanks again
    You're quite welcome.

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 10:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 08:04 PM
  4. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

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