Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:43:39+00:00 2026-06-02T18:43:39+00:00

Here’s my current code , and here’s my current output: This program will calculate

  • 0

Here’s my current code, and here’s my current output:

This program will calculate an overall score for a diver, based on individual dives.
The diver's score for dive 1 is 79.80. 
The diver's score for dive 2 is 49.80. 

The average score for these dives is 64.80. 

Here’s the page with the specifications.

Bascically this program takes a string from a file that looks like:

1 2.5 8.5 8.0 8.5 9.5 9.0 7.5 6.5

where the 1 is the dive number, the 2.5 is the difficulty, and the rest are the scores. I add up all of the scores except for the highest and lowest, multiply by the difficulty, and multiply by 0.6 to get my score for the dive.

What I’m having problems with is this: My dive 2 score is correct but my dive 1 score is incorrect. I’m really not quite sure why.

I’d really appreciate help; I’ve racked my brain to find out why it is this way but I can’t seem to figure it out. Thank you for looking, I appreciate it.

Edit: Here’s the code for you guys:

import java.util.*;
import java.io.*;

public class Dive {

    public static void main(String[] args) throws FileNotFoundException {
        printIntro();

        Scanner fileScanner = new Scanner(new File("DiveData.txt"));

        processDives(fileScanner);
    }

    public static void printIntro() {
        System.out
                .println("Welcome to the Diver Scoring program."
                        + "\nThis program will calculate an overall score for a diver, based on individual dives.");
    }

    public static void processDives(Scanner fileScanner) {
        double avg = 0;
        int count = 0;

        while (fileScanner.hasNext()) {
            int diveNumber = fileScanner.nextInt();
            String diveLine = fileScanner.nextLine();
            double score = calculateDiveScore(diveLine);
            avg += score;

            System.out.printf("The diver's score for dive " + diveNumber
                    + " is " + "%.2f. \n", score);

            count++;
            diveNumber = 0;
        }
        System.out.printf("\nThe average score for these dives is "
                + "%.2f. \n", avg / (double) count);
    }

    public static double calculateDiveScore(String diveLine) {
        double score = 0.0;
        String subNumbers = "";
        double difficulty = Double.parseDouble(diveLine.substring(1, 4));
        diveLine = diveLine.substring(5);
        double max = -500, min = 500;

        for (int i = 0; i < diveLine.length(); i++) {

            // if we're getting something other than a space, add it to the
            // string. We'll do the calculations while we have spaces.
            if (diveLine.charAt(i) != ' ') {
                subNumbers += diveLine.charAt(i);
            } else {
                double val = Double.parseDouble(subNumbers);
                // if the score is neither largest nor smallest, add it to total
                // score.
                if (Math.max(val, max) == max && Math.min(val, min) == min) {
                    score += val;
                }

                // if it is either largest or smallest, add previous largest or
                // smallest and set current to largest or smallest.
                if (Math.max(val, max) == val) {
                    if (max != -500)
                        score += max;
                    max = val;
                } else if (Math.min(val, min) == val) {
                    if (min != 500)
                        score += min;
                    min = val;
                }
                subNumbers = "";
            }
        }

        //check the last number to see if it's a max or a min
        if (Math.max(Double.parseDouble(subNumbers), max) == Double
                .parseDouble(subNumbers)) {
            score += max;
        } else if (Math.min(Double.parseDouble(subNumbers), min) == Double
                .parseDouble(subNumbers)) {
            score += min;
        }

        return score * difficulty * 0.6;
    }
}

And the answer can be found here:

import java.util.*;
import java.io.*;

public class Dive {

    public static void main(String[] args) throws FileNotFoundException {
        printIntro();

        Scanner fileScanner = new Scanner(new File("DiveData.txt"));

        processDives(fileScanner);
    }

    public static void printIntro() {
        System.out
                .println("Welcome to the Diver Scoring program."
                        + "\nThis program will calculate an overall score for a diver, based on individual dives.");
    }

    public static void processDives(Scanner fileScanner) {
        double avg = 0;
        int count = 0;

        while (fileScanner.hasNext()) {
            int diveNumber = fileScanner.nextInt();
            String diveLine = fileScanner.nextLine();
            double score = calculateDiveScore(diveLine);
            avg += score;

            System.out.printf("The diver's score for dive " + diveNumber
                    + " is " + "%.2f. \n", score);

            count++;
            diveNumber = 0;
        }
        System.out.printf("\nThe average score for these dives is " + "%.2f.",
                avg / (double) count);
    }

    public static double calculateDiveScore(String diveLine) {

        diveLine = diveLine.substring(1);
        String[] fields = diveLine.split(" ");
        double difficulty = Double.parseDouble(fields[0]);
        double[] scores = new double[fields.length - 1];
        double max = -500.0, min = 500.0, score = 0;

        for (int i = 0; i < fields.length - 1; i++) {
            scores[i] = Double.parseDouble(fields[i + 1]);
        }

        for (int i = 0; i < scores.length; i++) {
            if (Math.max(scores[i], max) == scores[i])
                max = scores[i];
            if (Math.min(scores[i], min) == scores[i])
                min = scores[i];
            score += scores[i];
        }

        score -= max;
        score -= min;

        return score * difficulty * 0.6;
    }
}

(Also found here on pastebin)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:43:41+00:00Added an answer on June 2, 2026 at 6:43 pm

    As this is homework, the best way for you to learn would probably to read the entire string in, then create a scanner over the String, then iterate over it with getDouble().

    So:

    for(iterate over number of lines) {
    
        String yourString = "blah";
    
        ... create Scanner pointing to yourString...
    
        while(scanner has a double) {
    
            double nextNum = get next double from scanner;
            ... Do something with that double...
        }
    }
    

    If you need any help on the specifics, feel free to ask.

    Edit: woops, missed that link to your code. Ignore the ‘post your code’ bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
here is code: <script type=text/javascript> function doit(){ $('table td').each(function () { if ($(this).text().trim() !=
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:
Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is an example: I write html code inside of textarea, then I swap
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is my code, which takes two version identifiers in the form 1, 5,
Here's a coding problem for those that like this kind of thing. Let's see

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.