| | |
first time using arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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:
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
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:
java Syntax (Toggle Plain Text)
//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
Java Syntax (Toggle Plain Text)
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[i] 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
Java Syntax (Toggle Plain Text)
if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
Well, that's enough for a quick glance at your code. Fix those points, then we can try to advance to the next step.
Last edited by masijade; Apr 5th, 2008 at 10:21 pm. Reason: typo
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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....
c++ Syntax (Toggle Plain Text)
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.
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.
Last edited by masijade; Apr 8th, 2008 at 2:38 am.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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
got kinda lost with that....besides this array stuff does my other stuff starting to look better
•
•
Join Date: Mar 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
------------------
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:
java Syntax (Toggle Plain Text)
//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
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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[i] = 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[i] < 0.0) && (score[i] > 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[i] < 0.0) && (score[i] > 10.0)) throw new NumberFormatException();
code is:
errors are:
C:\Documents and Settings\Don & Diane Kruep\My Documents\Judges.java:37: possible loss of precision
found : double
required: int
score[i] = 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[i] < 0.0) && (score[i] > 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[i] < 0.0) && (score[i] > 10.0)) throw new NumberFormatException();
code is:
java Syntax (Toggle Plain Text)
//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
Java Syntax (Toggle Plain Text)
for (double i = 0; i <= arraylength; i++)
array indexes are ints, not doubles. ;-)
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Jan 2008
Posts: 189
Reputation:
Solved Threads: 0
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:
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:
java Syntax (Toggle Plain Text)
//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
•
•
Join Date: Jan 2008
Posts: 3,833
Reputation:
Solved Threads: 503
DecimalFormat might come in handy here.
http://java.sun.com/j2se/1.5.0/docs/...malFormat.html
Here is an example.
http://www.daniweb.com/forums/post101822-4.html
http://java.sun.com/j2se/1.5.0/docs/...malFormat.html
Here is an example.
http://www.daniweb.com/forums/post101822-4.html
![]() |
Similar Threads
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Passing arrays of objects to functions (C++)
- java problem with creating arrays (Java)
- little help about arrays (C)
- Arrays (C++)
- Comparing elements of two seperate Arrays (PHP)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
- Arrays??? (Java)
- sorting 2d arrays (C)
Other Threads in the Java Forum
- Previous Thread: How to resize an image in JAVA
- Next Thread: java - missing return statement
| Thread Tools | Search this Thread |
Tag cloud for Java
android api appinventor apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jni jpanel julia linux list login loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






