Not exactly sure what I am doing wrong...it is probably something simple and just being an idiot today but I have these two errors in my program and not sure what I am doing wrong

C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:54: averageScores(double[]) in Judges cannot be applied to (double)
average = averageScores(score);
^
C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:67: operator + cannot be applied to double,double[]
total = total + g;


my code is:

//import classes
import java.io.*;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		float average;
		double score = 0.0;
		boolean done = false;

//Get input from user for grades and assign them to an array
		for (double i = 0; i <= 8; i++)
		{
			System.out.print("Enter score # 1:");
			System.out.print("Enter score # 2:");
			System.out.print("Enter score # 3:");
			System.out.print("Enter score # 4:");
			System.out.print("Enter score # 5:");
			System.out.print("Enter score # 6:");
			System.out.print("Enter score # 7:");
			System.out.print("Enter score # 8:");
			done = false;

			//use a while loop to keep for counter from increasing when invalid entry is made
			while(!done)
			{
				try
				{
					score = Double.parseDouble(dataIn.readLine());
					if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
					//reasonable check
					else done = true; //exit while is grade is valid
				}
				catch(NumberFormatException e)
				{
					System.out.print("** Invalid Entry ** \nPlease re-enter score");
				}
			} //end while
		} //end for

		//call a method to calculate the average
		average = averageScores(score);

		//Display grades and average
		System.out.println("");
				System.out.println("The average grade is " + average);
	}  //end main

	//method used to calculate the average
	public static double averageScores(double[] g)
		{
			double total = 0, a;

			for (double  i = 0; i<g.length; i++);
			total = total + g;
			a = total / g.length;

			return a;
		} //end averageGrades()
} //end class

Instructions say:

Arrays and For loops

In a diving competition, each contestant’s score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program (Judges.java) that allows the user to enter 8 judges’ scores into an array of doubles and then outputs the points earned by the contestant.

1. you can create either a command prompt console application or a console application using dialog boxes for this program
2. accept the user input as an array of doubles… accept 8 scores
3. only accept scores between 1.0 and 10.0 (include the endpoints)
4. make sure your user is entering valid scores only… if an invalid entry is made, prompt them with an error message and allow them to re-enter that score
5. in your prompt to the user (whether it is the original prompt or the prompt after an invalid entry was made), identify the number of the score they are entering (e.g., Enter score # 5:)
6. calculate the minimum and maximum scores using the functions from the Math class described in Chapter 3. (HINT: you can use these functions in a for loop to compare two numbers at a time to determine which is the max (or min))
7. calculate the total (the sum of the remaining 6 scores)
8. display the min, max, and total formatting each to two decimal places


Example:
Enter score # 1: 9.2
Enter score # 2: 9.3
Enter score # 3: 9.0
Enter score # 4: 9.9
Enter score # 5: 9.5
Enter score # 6: 9.5
Enter score # 7: 9.6
Enter score # 8: 9.8

The maximum score is 9.90
The minimum score is 9.00
The total score is 56.90

Recommended Answers

All 11 Replies

public static double averageScores(double[] g) {
        double total = 0, a;
        for (double  i = 0; i<g.length; i++);
        total = total + g;
        a = total / g.length;
        return a;
    } //end averageGrades()

In the above code, the line where you are declaring total (and I assume declaring "a") is wrong. Declare (and define with 0) each of them on a separate line.

You also realise (right) that your for loop does nothing, as it ends at the ";" that follows it.

And the next line, where you are suppossed to be doing the summing (inside of the for loop, but which at this point is outside of it) you are attempting to add together the double total, and the reference value of the array "g". I can only assume, that you meant to index into the array using the loop (i.e. g instead of g).

Also, you have defined your method to take an array, but the point at where you're calling it, your passing it a double. I assume you wanted to get, and store all scores before passing them to the method as an array all at one time, rather than one at a time, as you are doing now.

Also, on your if line

if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();

You do realise that a Number cannot be both less than 0 and greater than 10, right? I can only assume you meant to use or (||) here.

Well, that's enough for a quick glance at your code. Fix those points, then we can try to advance to the next step.

I am still trying to figure this program out.....is there a way to get the max and min score in an array....I am pretty sure I can do it in if statement but i think we should do an array....also could someone let me know what I am still doing wrong....

import java.io.*;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		float average;
		double score = 0.0;
		boolean done = false;

		//Get input from user for scores and assign them to an array
		for (double i = 0; i <= 8; i++)
			{
				System.out.print("Enter score # 1:");
				System.out.print("Enter score # 2:");
				System.out.print("Enter score # 3:");
				System.out.print("Enter score # 4:");
				System.out.print("Enter score # 5:");
				System.out.print("Enter score # 6:");
				System.out.print("Enter score # 7:");
				System.out.print("Enter score # 8:");

				done = false;

				while(!done)
					{
					try
						{
							score = Double.parseDouble(dataIn.readLine());
							if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();

							//reasonable check
							else done = true;
						}

						catch(NumberFormatException e)

						{
							System.out.print("** Invalid Entry ** \nPlease re-enter score");
						}
					} //end while
				} //end for

			//call a method to calculate the average
			average = averageScores(score);

			//Display grades and average
			System.out.println("");
			System.out.println("The average grade is " + average);

		} //end main

		//method used to calculate the average
		public static double averageScores(double[] g)
		{
			double total = 0.0;
			double a = 0.0;
			for (double i = 0; i < g; i++)
			{
			total = total + g;
			a = total / g;

			System.out.println("The maxiumum score is " + max_score);
			System.out.println("The minimum score is " + min_score);
			System.out.println("The total score is " + total);

			return a;
			}
		} //end averageScores()
} //end class

Arrays.sort, then take the first and last element for the min and max.

If you are not allowed to use that, and if it is an exercise you probably won't be. Simply declare two variables before your averaging loop, and initiate them with the first element in the array, then with each element compare the element to the variables giving the variables the value of the greater and lesser value.

i apologize for sounding kinda like a complete moron but pretty confused on the technical description of arrays "initiate them with the first element in the array, then with each element compare the element to the variables giving the variables the value of the greater and lesser value."

got kinda lost with that....besides this array stuff does my other stuff starting to look better

hi,
you have declared the variable "average" as a float, but trying to assign a value as Double,
either you declare average as a type double or convert return value (in method averageScores) to float.
try this, may work.
------------------


Not exactly sure what I am doing wrong...it is probably something simple and just being an idiot today but I have these two errors in my program and not sure what I am doing wrong

C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:54: averageScores(double[]) in Judges cannot be applied to (double)
average = averageScores(score);
^
C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:67: operator + cannot be applied to double,double[]
total = total + g;


my code is:

//import classes
import java.io.*;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		float average;
		double score = 0.0;
		boolean done = false;

//Get input from user for grades and assign them to an array
		for (double i = 0; i <= 8; i++)
		{
			System.out.print("Enter score # 1:");
			System.out.print("Enter score # 2:");
			System.out.print("Enter score # 3:");
			System.out.print("Enter score # 4:");
			System.out.print("Enter score # 5:");
			System.out.print("Enter score # 6:");
			System.out.print("Enter score # 7:");
			System.out.print("Enter score # 8:");
			done = false;

			//use a while loop to keep for counter from increasing when invalid entry is made
			while(!done)
			{
				try
				{
					score = Double.parseDouble(dataIn.readLine());
					if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
					//reasonable check
					else done = true; //exit while is grade is valid
				}
				catch(NumberFormatException e)
				{
					System.out.print("** Invalid Entry ** \nPlease re-enter score");
				}
			} //end while
		} //end for

		//call a method to calculate the average
		average = averageScores(score);

		//Display grades and average
		System.out.println("");
				System.out.println("The average grade is " + average);
	}  //end main

	//method used to calculate the average
	public static double averageScores(double[] g)
		{
			double total = 0, a;

			for (double  i = 0; i<g.length; i++);
			total = total + g;
			a = total / g.length;

			return a;
		} //end averageGrades()
} //end class

Instructions say:

Arrays and For loops

In a diving competition, each contestant’s score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program (Judges.java) that allows the user to enter 8 judges’ scores into an array of doubles and then outputs the points earned by the contestant.

1. you can create either a command prompt console application or a console application using dialog boxes for this program
2. accept the user input as an array of doubles… accept 8 scores
3. only accept scores between 1.0 and 10.0 (include the endpoints)
4. make sure your user is entering valid scores only… if an invalid entry is made, prompt them with an error message and allow them to re-enter that score
5. in your prompt to the user (whether it is the original prompt or the prompt after an invalid entry was made), identify the number of the score they are entering (e.g., Enter score # 5:)
6. calculate the minimum and maximum scores using the functions from the Math class described in Chapter 3. (HINT: you can use these functions in a for loop to compare two numbers at a time to determine which is the max (or min))
7. calculate the total (the sum of the remaining 6 scores)
8. display the min, max, and total formatting each to two decimal places


Example:
Enter score # 1: 9.2
Enter score # 2: 9.3
Enter score # 3: 9.0
Enter score # 4: 9.9
Enter score # 5: 9.5
Enter score # 6: 9.5
Enter score # 7: 9.6
Enter score # 8: 9.8

The maximum score is 9.90
The minimum score is 9.00
The total score is 56.90

significant changes with three errors dealing with one issue...trying to figure out just why this is not compiling right and I know it has something to do with declaring int and double just can't see what tho:

errors are:

C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:37: possible loss of precision
found : double
required: int
score = Double.parseDouble(dataIn.readLine());
^
C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:38: possible loss of precision
found : double
required: int
if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
^
C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:38: possible loss of precision
found : double
required: int
if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();


code is:

//import classes
import java.io.*;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		int arraylength = 8;
		double average;
		double score[] = new double [arraylength];
		boolean done = false;

		//Get input from user for scores and assign them to an array
		for (double i = 0; i <= arraylength; i++)
			{
				System.out.print("Enter score # " + (i+1) + ":");


				done = false;

				while(!done)
					{
					try
						{
							score[i] = Double.parseDouble(dataIn.readLine());
							if ((score[i] < 0.0) && (score[i] > 10.0)) throw new NumberFormatException();

							//reasonable check
							else done = true;
						}

						catch(NumberFormatException e)

						{
							System.out.print("** Invalid Entry ** \nPlease re-enter score");
						}
					} //end while
				} //end for

			//call a method to calculate the average
			average = averageScores(score);

			//Display grades and average
			System.out.println("");
			System.out.println("The average grade is " + average);

		} //end main

		//method used to calculate the average
		public static double averageScores(double[] g)
		{
			double total = 0.0;
			double a = 0.0;
			double max_score = 0.0;
			double min_score = 0.0;
			for (int i = 0; i < g.length; i++)
			{
				total = total + g[i];
				if (g[i] < min_score)
				{
					min_score = g[i];
				}
				else
				{
					if (g[i] > max_score)
					{
						 max_score = g[i];
					}
				}
			}

			a = total / g.length;

			System.out.println("The maxiumum score is " + max_score);
			System.out.println("The minimum score is " + min_score);
			System.out.println("The total score is " + total);

			return a;

		} //end averageScores()
} //end class
for (double i = 0; i <= arraylength; i++)

array indexes are ints, not doubles. ;-)

One last thing I need my decimals for 2 digits and I know in c++ it is

cout.setf(ios::fixed)
cout.setf(ios::showpoint)
cout.precision(2)

but for java not sure what it is or where it should go for my code:

//import classes
import java.io.*;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		int arraylength = 8;
		double average;
		double score[] = new double [arraylength];
		boolean done = false;

		//Get input from user for scores and assign them to an array
		for (int i = 0; i < arraylength; i++)
			{
				System.out.print("Enter score # " + (i+1) + ": ");


				done = false;

				while(!done)
					{
					try
						{
							score[i] = Double.parseDouble(dataIn.readLine());
							if ((score[i] < 0.0) || (score[i] > 10.0)) throw new NumberFormatException();

							//reasonable check
							else done = true;
						}

						catch(NumberFormatException e)

						{
							System.out.print("** Invalid Entry ** \nPlease re-enter score # " + (i + 1) + ": ");
						}
					} //end while
				} //end for

			//call a method to calculate the average
			average = averageScores(score);

		} //end main

		//method used to calculate the average
		public static double averageScores(double[] g)
		{
			double total = 0.0;
			double max_score = 0.0;
			double min_score = 10.0;
			for (int i = 0; i < g.length; i++)
			{
				total = total + g[i];
				if (g[i] < min_score)
				{
					min_score = g[i];
				}

					if (g[i] > max_score)
					{
						 max_score = g[i];
					}
				}

			total = total - (max_score + min_score);

			System.out.println("");
			System.out.println("");
			System.out.println("The maxiumum score is " + max_score);
			System.out.println("The minimum score is " + min_score);
			System.out.println("The total score is " + total);

			return total;

		} //end averageScores()
} //end class

this is what I am doing and still not giving me an extra 0

//import classes
import java.io.*;
import java.text.DecimalFormat;

public class Judges
{
	public static void main(String[] args) throws IOException
	{
		//declaring variables
		BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
		int arraylength = 8;
		double average;
		double score[] = new double [arraylength];
		boolean done = false;

		//Get input from user for scores and assign them to an array
		for (int i = 0; i < arraylength; i++)
			{
				System.out.print("Enter score # " + (i+1) + ": ");


				done = false;

				while(!done)
					{
					try
						{
							score[i] = Double.parseDouble(dataIn.readLine());
							if ((score[i] < 0.0) || (score[i] > 10.0)) throw new NumberFormatException();

							//reasonable check
							else done = true;
						}

						catch(NumberFormatException e)

						{
							System.out.print("** Invalid Entry ** \nPlease re-enter score # " + (i + 1) + ": ");
						}
					} //end while
				} //end for

			//call a method to calculate the average
			average = averageScores(score);

		} //end main



		//method used to calculate the average
		public static double averageScores(double[] g)
		{
			DecimalFormat twoDigits = new DecimalFormat("#.##");
			double total = 0.0;
			double max_score = 0.0;
			double min_score = 10.0;
			for (int i = 0; i < g.length; i++)
			{
				total = total + g[i];
				if (g[i] < min_score)
				{
					min_score = g[i];
				}

					if (g[i] > max_score)
					{
						 max_score = g[i];
					}
				}

			total = total - (max_score + min_score);




			System.out.println("");
			System.out.println("");
			System.out.println("The maxiumum score is " + max_score);
			System.out.println("The minimum score is " + min_score);
			System.out.println("The total score is " + total);

			return total;

		} //end averageScores()
} //end class

DecimalFormat("#.##") will show up to 2 decimal places if applicable. If you want to always show 2 digits after the decimal use DecimalFormat("#.00") Edit: Just noticed, but you are not even using the DecimalFormat you created. You need to use that instance to format your numbers like so

System.out.println("The maxiumum score is " + twoDigits.format(max_score) );

(ditch the 8-space tab indent too - it's just painful to read)

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.