# Help with filling an array

• 03-23-2011, 03:11 PM
MaceMan
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:

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);     } }```
I know I should have used methods, but I am supposed to do this problem without. When I compile the code I have no errors, when I run the code, I get the runtime error:

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.
• 03-23-2011, 03:21 PM
sunde887
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
• 03-23-2011, 03:21 PM
JosAH
Quote:

Originally Posted by MaceMan
Code:

`                for (index = 1; index < 7; index++);`

Remove that trailing semicolon; there are more issues with the code but I leave them be for now.

kind regards,

Jos
• 03-23-2011, 03:33 PM
MaceMan
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));
• 03-23-2011, 03:48 PM
JosAH
Quote:

Originally Posted by MaceMan
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));

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,

Jos
• 03-23-2011, 03:57 PM
MaceMan
Okay, thanks Jos. I can say that I am now going to be a regular at this java forums.
Thanks for all the help!
• 03-23-2011, 04:10 PM
JosAH
Quote:

Originally Posted by MaceMan
Okay, thanks Jos. I can say that I am now going to be a regular at this java forums.
Thanks for all the help!

You're welcome of course.

kind regards,

Jos