Results 1 to 7 of 7
Thread: Help with filling an array
- 03-23-2011, 02:11 PM #1
Member
- 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:
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: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
- 03-23-2011, 02:21 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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, 02:21 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 03-23-2011, 02:33 PM #4
Member
- 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; 03-23-2011 at 02:44 PM.
When in doubt, use a lightsaber
- 03-23-2011, 02:48 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-23-2011, 02:57 PM #6
Member
- 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
- 03-23-2011, 03:10 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
filling an array of Address objects..?
By hiei_yasha in forum New To JavaReplies: 9Last Post: 02-20-2011, 11:25 PM -
Filling with transparency
By zirbinator in forum Java 2DReplies: 3Last Post: 02-09-2011, 02:36 AM -
Filling an array from the return value of the function
By alex1988 in forum Java AppletsReplies: 7Last Post: 02-02-2011, 09:29 AM -
Filling a JTable
By aborgeld in forum Advanced JavaReplies: 0Last Post: 01-08-2011, 01:37 PM -
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11-12-2008, 12:43 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks