Results 1 to 6 of 6
Thread: I need help with this program :P
- 09-29-2011, 10:44 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
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?
And then I get this error: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] + ", "); } } }
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 05:00 PM. Reason: Added code tags, [code]...[/code]
- 09-29-2011, 05:03 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: I need help with this program :P
Inspect this section, perhaps print the value of i. There is another problem with this as well.Java Code:for(int j=0; j<averageAge.length; j++); System.out.print(averageAge[i] + ", ");
- 10-04-2011, 01:28 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 2
- Rep Power
- 0
Re: I need help with this program :P
I´ve checked in to that, but I still can´t make it work ... And I need to be finished
before friday :(
Does anyone have another idea?
Thank you for your reply :)
- 10-04-2011, 03:23 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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?
- 10-13-2011, 10:38 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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] + ", ");
- 10-13-2011, 06:35 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks