first time using arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

first time using arrays

 
0
  #1
Apr 5th, 2008
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:

  1.  
  2. //import classes
  3. import java.io.*;
  4.  
  5. public class Judges
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //declaring variables
  10. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  11. float average;
  12. double score = 0.0;
  13. boolean done = false;
  14.  
  15. //Get input from user for grades and assign them to an array
  16. for (double i = 0; i <= 8; i++)
  17. {
  18. System.out.print("Enter score # 1:");
  19. System.out.print("Enter score # 2:");
  20. System.out.print("Enter score # 3:");
  21. System.out.print("Enter score # 4:");
  22. System.out.print("Enter score # 5:");
  23. System.out.print("Enter score # 6:");
  24. System.out.print("Enter score # 7:");
  25. System.out.print("Enter score # 8:");
  26. done = false;
  27.  
  28. //use a while loop to keep for counter from increasing when invalid entry is made
  29. while(!done)
  30. {
  31. try
  32. {
  33. score = Double.parseDouble(dataIn.readLine());
  34. if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
  35. //reasonable check
  36. else done = true; //exit while is grade is valid
  37. }
  38. catch(NumberFormatException e)
  39. {
  40. System.out.print("** Invalid Entry ** \nPlease re-enter score");
  41. }
  42. } //end while
  43. } //end for
  44.  
  45. //call a method to calculate the average
  46. average = averageScores(score);
  47.  
  48. //Display grades and average
  49. System.out.println("");
  50. System.out.println("The average grade is " + average);
  51. } //end main
  52.  
  53. //method used to calculate the average
  54. public static double averageScores(double[] g)
  55. {
  56. double total = 0, a;
  57.  
  58. for (double i = 0; i<g.length; i++);
  59. total = total + g;
  60. a = total / g.length;
  61.  
  62. return a;
  63. } //end averageGrades()
  64. } //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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,448
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: first time using arrays

 
0
  #2
Apr 5th, 2008
  1. public static double averageScores(double[] g) {
  2. double total = 0, a;
  3. for (double i = 0; i<g.length; i++);
  4. total = total + g;
  5. a = total / g.length;
  6. return a;
  7. } //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
  1. 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.
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: first time using arrays

 
0
  #3
Apr 8th, 2008
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....

  1.  
  2. import java.io.*;
  3.  
  4. public class Judges
  5. {
  6. public static void main(String[] args) throws IOException
  7. {
  8. //declaring variables
  9. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  10. float average;
  11. double score = 0.0;
  12. boolean done = false;
  13.  
  14. //Get input from user for scores and assign them to an array
  15. for (double i = 0; i <= 8; i++)
  16. {
  17. System.out.print("Enter score # 1:");
  18. System.out.print("Enter score # 2:");
  19. System.out.print("Enter score # 3:");
  20. System.out.print("Enter score # 4:");
  21. System.out.print("Enter score # 5:");
  22. System.out.print("Enter score # 6:");
  23. System.out.print("Enter score # 7:");
  24. System.out.print("Enter score # 8:");
  25.  
  26. done = false;
  27.  
  28. while(!done)
  29. {
  30. try
  31. {
  32. score = Double.parseDouble(dataIn.readLine());
  33. if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
  34.  
  35. //reasonable check
  36. else done = true;
  37. }
  38.  
  39. catch(NumberFormatException e)
  40.  
  41. {
  42. System.out.print("** Invalid Entry ** \nPlease re-enter score");
  43. }
  44. } //end while
  45. } //end for
  46.  
  47. //call a method to calculate the average
  48. average = averageScores(score);
  49.  
  50. //Display grades and average
  51. System.out.println("");
  52. System.out.println("The average grade is " + average);
  53.  
  54. } //end main
  55.  
  56. //method used to calculate the average
  57. public static double averageScores(double[] g)
  58. {
  59. double total = 0.0;
  60. double a = 0.0;
  61. for (double i = 0; i < g; i++)
  62. {
  63. total = total + g;
  64. a = total / g;
  65.  
  66. System.out.println("The maxiumum score is " + max_score);
  67. System.out.println("The minimum score is " + min_score);
  68. System.out.println("The total score is " + total);
  69.  
  70. return a;
  71. }
  72. } //end averageScores()
  73. } //end class
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,448
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: first time using arrays

 
0
  #4
Apr 8th, 2008
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.
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: first time using arrays

 
0
  #5
Apr 8th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 5
Reputation: ratnasatish is an unknown quantity at this point 
Solved Threads: 0
ratnasatish ratnasatish is offline Offline
Newbie Poster

Re: first time using arrays

 
0
  #6
Apr 8th, 2008
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.
------------------
Originally Posted by jimJohnson View Post



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:

  1.  
  2. //import classes
  3. import java.io.*;
  4.  
  5. public class Judges
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //declaring variables
  10. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  11. float average;
  12. double score = 0.0;
  13. boolean done = false;
  14.  
  15. //Get input from user for grades and assign them to an array
  16. for (double i = 0; i <= 8; i++)
  17. {
  18. System.out.print("Enter score # 1:");
  19. System.out.print("Enter score # 2:");
  20. System.out.print("Enter score # 3:");
  21. System.out.print("Enter score # 4:");
  22. System.out.print("Enter score # 5:");
  23. System.out.print("Enter score # 6:");
  24. System.out.print("Enter score # 7:");
  25. System.out.print("Enter score # 8:");
  26. done = false;
  27.  
  28. //use a while loop to keep for counter from increasing when invalid entry is made
  29. while(!done)
  30. {
  31. try
  32. {
  33. score = Double.parseDouble(dataIn.readLine());
  34. if ((score < 0.0) && (score > 10.0)) throw new NumberFormatException();
  35. //reasonable check
  36. else done = true; //exit while is grade is valid
  37. }
  38. catch(NumberFormatException e)
  39. {
  40. System.out.print("** Invalid Entry ** \nPlease re-enter score");
  41. }
  42. } //end while
  43. } //end for
  44.  
  45. //call a method to calculate the average
  46. average = averageScores(score);
  47.  
  48. //Display grades and average
  49. System.out.println("");
  50. System.out.println("The average grade is " + average);
  51. } //end main
  52.  
  53. //method used to calculate the average
  54. public static double averageScores(double[] g)
  55. {
  56. double total = 0, a;
  57.  
  58. for (double i = 0; i<g.length; i++);
  59. total = total + g;
  60. a = total / g.length;
  61.  
  62. return a;
  63. } //end averageGrades()
  64. } //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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: first time using arrays

 
0
  #7
Apr 8th, 2008
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:

  1.  
  2. //import classes
  3. import java.io.*;
  4.  
  5. public class Judges
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //declaring variables
  10. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  11. int arraylength = 8;
  12. double average;
  13. double score[] = new double [arraylength];
  14. boolean done = false;
  15.  
  16. //Get input from user for scores and assign them to an array
  17. for (double i = 0; i <= arraylength; i++)
  18. {
  19. System.out.print("Enter score # " + (i+1) + ":");
  20.  
  21.  
  22. done = false;
  23.  
  24. while(!done)
  25. {
  26. try
  27. {
  28. score[i] = Double.parseDouble(dataIn.readLine());
  29. if ((score[i] < 0.0) && (score[i] > 10.0)) throw new NumberFormatException();
  30.  
  31. //reasonable check
  32. else done = true;
  33. }
  34.  
  35. catch(NumberFormatException e)
  36.  
  37. {
  38. System.out.print("** Invalid Entry ** \nPlease re-enter score");
  39. }
  40. } //end while
  41. } //end for
  42.  
  43. //call a method to calculate the average
  44. average = averageScores(score);
  45.  
  46. //Display grades and average
  47. System.out.println("");
  48. System.out.println("The average grade is " + average);
  49.  
  50. } //end main
  51.  
  52. //method used to calculate the average
  53. public static double averageScores(double[] g)
  54. {
  55. double total = 0.0;
  56. double a = 0.0;
  57. double max_score = 0.0;
  58. double min_score = 0.0;
  59. for (int i = 0; i < g.length; i++)
  60. {
  61. total = total + g[i];
  62. if (g[i] < min_score)
  63. {
  64. min_score = g[i];
  65. }
  66. else
  67. {
  68. if (g[i] > max_score)
  69. {
  70. max_score = g[i];
  71. }
  72. }
  73. }
  74.  
  75. a = total / g.length;
  76.  
  77. System.out.println("The maxiumum score is " + max_score);
  78. System.out.println("The minimum score is " + min_score);
  79. System.out.println("The total score is " + total);
  80.  
  81. return a;
  82.  
  83. } //end averageScores()
  84. } //end class
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,448
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 261
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: first time using arrays

 
0
  #8
Apr 8th, 2008
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 189
Reputation: jimJohnson is an unknown quantity at this point 
Solved Threads: 0
jimJohnson jimJohnson is offline Offline
Junior Poster

Re: first time using arrays

 
0
  #9
Apr 8th, 2008
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:

  1.  
  2. //import classes
  3. import java.io.*;
  4.  
  5. public class Judges
  6. {
  7. public static void main(String[] args) throws IOException
  8. {
  9. //declaring variables
  10. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  11. int arraylength = 8;
  12. double average;
  13. double score[] = new double [arraylength];
  14. boolean done = false;
  15.  
  16. //Get input from user for scores and assign them to an array
  17. for (int i = 0; i < arraylength; i++)
  18. {
  19. System.out.print("Enter score # " + (i+1) + ": ");
  20.  
  21.  
  22. done = false;
  23.  
  24. while(!done)
  25. {
  26. try
  27. {
  28. score[i] = Double.parseDouble(dataIn.readLine());
  29. if ((score[i] < 0.0) || (score[i] > 10.0)) throw new NumberFormatException();
  30.  
  31. //reasonable check
  32. else done = true;
  33. }
  34.  
  35. catch(NumberFormatException e)
  36.  
  37. {
  38. System.out.print("** Invalid Entry ** \nPlease re-enter score # " + (i + 1) + ": ");
  39. }
  40. } //end while
  41. } //end for
  42.  
  43. //call a method to calculate the average
  44. average = averageScores(score);
  45.  
  46. } //end main
  47.  
  48. //method used to calculate the average
  49. public static double averageScores(double[] g)
  50. {
  51. double total = 0.0;
  52. double max_score = 0.0;
  53. double min_score = 10.0;
  54. for (int i = 0; i < g.length; i++)
  55. {
  56. total = total + g[i];
  57. if (g[i] < min_score)
  58. {
  59. min_score = g[i];
  60. }
  61.  
  62. if (g[i] > max_score)
  63. {
  64. max_score = g[i];
  65. }
  66. }
  67.  
  68. total = total - (max_score + min_score);
  69.  
  70. System.out.println("");
  71. System.out.println("");
  72. System.out.println("The maxiumum score is " + max_score);
  73. System.out.println("The minimum score is " + min_score);
  74. System.out.println("The total score is " + total);
  75.  
  76. return total;
  77.  
  78. } //end averageScores()
  79. } //end class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,833
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: first time using arrays

 
0
  #10
Apr 9th, 2008
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC