Results 1 to 7 of 7
Thread: Help with filling an array
 03232011, 03:11 PM #1Member
 Join Date
 Mar 2011
 Posts
 45
 Rep Power
 0
Help with filling an array
There is my problem:
In the sport of diving, seven judges award a score between 0 and 10, where each score may be a floating point value. The highest and lowest scores are thrown out and the remaining scores are added together. The sum is then multiplied by the degree of difficulty for that dive. The degree of difficulty ranges from 1.2 to 3.8 points. The total is then multiplied by 0.6 to determine the diver's score.
Write a computer program that inputs a degree of difficulty and seven judges' scores and outputs the overall score for that dive. The program should ensure that all inputs are within the allowable data ranges.
Below is my code:
Java Code:import java.util.Arrays; import java.util.Scanner; public class DivingScores { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double[] scores = new double[7]; double max; double sum = 0; double difficulty; double finalscore; int index; System.out.println("Enter " + scores.length + " scores."); scores[0] = keyboard.nextDouble(); max = scores[0]; for (index = 1; index < 7; index++); { scores[index] = keyboard.nextDouble(); if (scores[index] > max) max = scores[index]; } Arrays.sort(scores); for(int i=0; i < scores.length ; i++) { sum = sum + scores[i]; } System.out.println("Enter degree of difficulty"); difficulty = keyboard.nextDouble(); double S0 = scores[0]; double S1 = scores[1]; double S2 = scores[2]; double S3 = scores[3]; double S4 = scores[4]; double S5 = scores[5]; double S6 = scores[6]; finalscore = ((((sum  scores[0])  scores[6]) * difficulty) * 0.6); System.out.println("final score of diver is " + finalscore); } }
Enter 7 scores.
1 2 3 4 5 6 7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at DivingScores.main(DivingScores.java:30)
Process completed.
Any help of why there is an error will be appreciated.
Thanks.When in doubt, use a lightsaber
 03232011, 03:21 PM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
the error tells you whats wrong.
Arrays are 0 based, a 7 item array has the following indices
0, 1, 2, 3, 4, 5, 6
 03232011, 03:21 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,126
 Blog Entries
 7
 Rep Power
 24
 03232011, 03:33 PM #4Member
 Join Date
 Mar 2011
 Posts
 45
 Rep Power
 0
Thank you very much, removing the semicolon fixed the error. Thanks a ton and a hlaf.
I have two more questions, I don't need to have the;
double S0 = scores[0];
double S1 = scores[1];
double S2 = scores[2];
double S3 = scores[3];
double S4 = scores[4];
double S5 = scores[5];
double S6 = scores[6];
Do I? It seems to me that it does nothing.
Last question:
The program outputs the correct answer, (I verifyied it with manual math) both outputs the answer with too many decimal places. For example:
scores = 1.2 3 2 4 5 6 5
degree = 2.3
final score = 26.2299999995
I want to format that to only two decimal places, so would I have to use something like this?
DecimalFormat formattingObject = new Decimalformat("#0.00#");
System.out.printlnm("Final score is " + formattingObject.format(finalScore));Last edited by MaceMan; 03232011 at 03:44 PM.
When in doubt, use a lightsaber
 03232011, 03:48 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,126
 Blog Entries
 7
 Rep Power
 24
Indeed you don't need those doubles; a.a.m.o.f. you only need to add the array elements at position 1, 2, 3, 4 and 5 because you can leave out the smallest and largest elements. Also, there is no need to keep track of a maximum before sorting the array. Have a look at the System.out.printf( ... ) method if you want to format the double numbers.
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 03232011, 03:57 PM #6Member
 Join Date
 Mar 2011
 Posts
 45
 Rep Power
 0
Okay, thanks Jos. I can say that I am now going to be a regular at this java forums.
Thanks for all the help!When in doubt, use a lightsaber
 03232011, 04:10 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,126
 Blog Entries
 7
 Rep Power
 24
Similar Threads

filling an array of Address objects..?
By hiei_yasha in forum New To JavaReplies: 9Last Post: 02212011, 12:25 AM 
Filling with transparency
By zirbinator in forum Java 2DReplies: 3Last Post: 02092011, 03:36 AM 
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02022011, 10:29 AM 
Filling a JTable
By aborgeld in forum Advanced JavaReplies: 0Last Post: 01082011, 02:37 PM 
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11122008, 01:43 PM
Bookmarks