Results 1 to 7 of 7
  1. #1
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default 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);
        }
    }
    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.
    When in doubt, use a lightsaber

  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

    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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by MaceMan View Post
    Java 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
    Last edited by JosAH; 03-23-2011 at 02:24 PM.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by MaceMan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    MaceMan is offline Member
    Join Date
    Mar 2011
    Posts
    45
    Rep Power
    0

    Default

    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

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by MaceMan View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. filling an array of Address objects..?
    By hiei_yasha in forum New To Java
    Replies: 9
    Last Post: 02-20-2011, 11:25 PM
  2. Filling with transparency
    By zirbinator in forum Java 2D
    Replies: 3
    Last Post: 02-09-2011, 02:36 AM
  3. Filling an array from the return value of the function
    By alex1988 in forum Java Applets
    Replies: 7
    Last Post: 02-02-2011, 09:29 AM
  4. Filling a JTable
    By aborgeld in forum Advanced Java
    Replies: 0
    Last Post: 01-08-2011, 01:37 PM
  5. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 12:43 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
  •