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

Thread: Passing array method error

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing array method error

    Trying to write a method that will pass an array of ints that counts the number of values that are larger than the average.
    Getting an out of bounds exception for the method along with an error when trying to invoke Methods.counts(array1);

    Tried minus/plus 1 for the array but still not working.

    Can anybody see the error?

    Thanks.

    Compiler error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Methods.counts(Methods.java:16)
    at Main.main(Main.java:8)

    public class Methods {	
     
    	public static int counts(int[] array)
    	{
    		int sum = 0;
    		int count = 0;
     
    		for (int i = 0; i < array.length; i++)	
    		{
    			sum = sum + array[i];			
    			double average = sum/array.length;
     
    			for (int j = 0; j < array.length; i++)	
    			{
    				if (array[i] > average)	
    				{
    					count++;					
    				}
    			}
    		}
    		return count;
    	}	
    }

    public class Main {
     
     
    	public static void main(String[] args) {
     
    		int[] array1 = {1,2,3,4,5,10};
    		Methods.counts(array1);
    		System.out.println(Methods.counts(array1));
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Passing array method error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Methods.counts(Methods.java:16)
    The value in the index at line 16 goes past the end of the array.
    Remember array index values range from 0 to the array length-1

    Check the code to see how the index got past the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing array method error

    Made a couple of silly mistakes. The working code below.

    Thanks.

    public class Methods {	
     
    	public int counts(int[] array)
    	{
    		int sum = 0;
    		int count = 0;		
     
    		for (int i = 0; i < array.length; i++)	
    		{			
    			sum = sum + array[i];	
    		}
    			double average = sum/array.length;
     
    			for (int i = 0; i < array.length; i++)	{				
    				if (array[i] > average)	
    				{					
    					count++;
    				}					
    			}			
    		return count;
    	}	
    }

    public class Main {
     
     
    	public static void main(String[] args) {
     
    		int[] array1 = {1,4,6,2,4,7,8,9};
    		Methods m1 = new Methods();
    		int result = m1.counts(array1);
    		System.out.println("Numbers above the average: " +result);
    	}
    }

Similar Threads

  1. [SOLVED] Passing 2D array to method
    By drgroove777 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2012, 02:08 AM
  2. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 03:27 PM
  3. method not passing across actionlistener interface
    By flamewolf393 in forum AWT / Java Swing
    Replies: 9
    Last Post: November 16th, 2011, 07:16 AM
  4. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  5. passing string into method
    By tabutcher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 08:43 AM