import java.io.FileReader;
import java.util.Scanner;



public class diverScores
{


	public static void main(String[] args)
    {

		Scanner kb = new Scanner(System.in);
		double [] score = new double [8];
		double totalScore = 0.00;

		Scanner fileScanner=null;
		try{
		fileScanner= new Scanner(new FileReader("divingdata.txt"));
	    }catch(Exception e)
	    {
			System.out.println("Input file not found");
			System.exit(1);
	    }
	    while(fileScanner.hasNext())
	    {
			String fileLine = fileScanner.nextLine();
			String diverName = fileLine.substring(0,fileLine.indexOf("-"));
			String strScore = fileLine.substring(fileLine.indexOf("-")+2, fileLine.length());
			String strScoreArray[] = strScore.split(" ");

			for(int i =0; i<strScoreArray.length; i++)
			{
				score [i] = Double.parseDouble(strScoreArray [i]);
		    }
		    totalScore = calculateScore(score,8);
		    totalScore = totalScore - highScore(score, 8) - lowScore(score, 8);

		    System.out.printf((diverName) +" - "+ (totalScore%.2f) + "points\n");
	    }

    }


private static double highScore(double[] score, int maxCount)
{

         double highScore = 0;
         highScore = score[0];
         for(int i=0;i<maxCount;i++)
         {

               if (score[i] > highScore) {
                    highScore = score[i];
               }
         }
         return highScore;
}

     private static double lowScore(double[] score, int maxCount)
     {
         double lowScore = 0;
         lowScore = score[0];
         for(int i=0;i<maxCount;i++)
         {

                 //Compare the current score with the high score.


               if (score[i] < lowScore) {
                    lowScore = score[i];
               }
          }
         return lowScore;
     }


      //calculateScore function for calculating the score from the array

     private static double calculateScore(double [ ] score,int maxCount)
     {


           // Sum the scores,

          double total = 0.00;
          for (int i = 0; i < maxCount; i++) {
                 total = total + score[i];
          }
          return total;
     }

}

Recommended Answers

All 2 Replies

Can you provide some more information, Like,

What you did
When you got this exception i.e. while compiling or at runtime?

These thigs will help us to understand your problem easily and help you quickly.

String diverName = fileLine.substring(0,fileLine.indexOf("-"));
String strScore = fileLine.substring(fileLine.indexOf("-")+2, fileLine.length());

Guess what happens if there is no "-" in any particular line. ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.