Results 1 to 6 of 6
  1. #1
    jonny007 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default I need help with this program :P

    I have an assigment for school, but I simply can't make it work. Does anyone here
    have an idea of what Im doing wrong?

    Java Code:
    import java.util.Scanner;
    
    public class Del2Oppgave1
    {
    	public static void main(String[] args)
    	{
    		int[] averageAge=new int[10];
    		int i, sumAge=0;
    		int answer=1;
    		double averageNewAge;
    		Scanner input=new Scanner(System.in);
    
    		
    		for(i=0; i<=9; i++)
    		{
    			System.out.print("Enter the persons age: "+(i+1)+": ");
    			averageAge[i]=input.nextInt();
    			sumAge=sumAge+averageAge[i];
    		}
    			averageNewAge=(double)sumAge/i;
    			System.out.println("\nAverage age is: "+averageNewAge);
    			
    		System.out.println("Would you like to change any of the values? 1 for yes and 2 for no");
    		answer=input.nextInt();
    			
    		if(answer==1)
    		{
    			System.out.println("What index would you like to change (0-9)");
    			int index=input.nextInt();
    			
    			System.out.println("What will the new value be?");
    			int value=input.nextInt();
    			averageAge[index]=value;
    			
    			for(int j=0; j<averageAge.length; j++);
    				System.out.print(averageAge[i] + ", ");
    		}
    	}
    
    }
    And then I get this error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Del2Oppgave1.main(Del2Oppgave1.java:36)

    I want the new value be placed in the array instead of the previous. How do I do this?

    Thank you for any help :)
    Last edited by sunde887; 09-29-2011 at 06:00 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: I need help with this program :P

    Java Code:
    for(int j=0; j<averageAge.length; j++);
    				System.out.print(averageAge[i] + ", ");
    Inspect this section, perhaps print the value of i. There is another problem with this as well.

  3. #3
    jonny007 is offline Member
    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0

    Default Re: I need help with this program :P

    Ive checked in to that, but I still cant make it work ... And I need to be finished
    before friday :(

    Does anyone have another idea?

    Thank you for your reply :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: I need help with this program :P

    Why are you declaring j in the loop counter and not using it? Is i below the length of the loop?

  5. #5
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: I need help with this program :P

    Instead below lines:

    for(int j=0; j<averageAge.length; j++);
    System.out.print(averageAge[i] + ", ");

    Try these :

    for(int j=0; j<averageAge.length; j++);
    System.out.print(averageAge[j] + ", ");

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default Re: I need help with this program :P

    The error you're getting is from simply using the wrong variable in your array index. But you have other problems:

    1) averageAge[] is a terrible name for your array. It's not an array of average ages, it's an array of ages.

    2) If you compute the mean of the values you collect before you allow your user to change one or more of those values, you're going to end up with an inaccurate mean.

    3) Does the user really get only one chance to change a value?

    4) Don't you want to print out the list of values before you ask the user whether to change them?

    That's not everything, but it should give you a few things to think about.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •