Results 1 to 2 of 2

Thread: new to arrays

  1. #1
    jimJohnson is offline Member
    Join Date
    Jan 2008
    Posts
    22
    Rep Power
    0

    Default new to arrays

    I have writing a program with a few errors...One thing I am having trouble with is figuring out arrays....also I am trying to get the min and max value figured out using the math function and for loop but havin an awful time trying to figure this out...

    my code is:

    Java Code:
    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
    instructions are:

    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


    I am completely stuck and any help id really really appreciate

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Java Code:
    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");
            }
          } // 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 max_score = g[0], min_score = g[0];
        double total = 0.0;
        double a = 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
    Things I changed and why...

    1) Use integers for your for loop counters unless you specifically need a double this allows you to use the counter as a current array location.

    2) The way you had it placed all of your "Enter score x" each time through the loop. Just use one and make it dynamic.

    3) I added a variable "arraylength" so you can change the size of the array with one number.

    4) Arrays are declared by putting "[]" after the datatype. Then reference a specific array location by using ["desired array location"] but remember that arrays count starting from 0, so the first location is 0 and the last location is your length-1.

    5) Pick one, doubles or floats. If you start mixing them you will get erros like when you returned the average as a double and tried to store it as a float.

    6) I do not know of a way to get the min or max value of an unsorted array, so a quick addition of an if statement in your for loop finds them both.

    7) Again, some things should stay outside your loop to make sure they arent executed each iteration like the System.out stuff and the calculation of your average.

    8) array.length gives you the number of elements it contains.

    Check out this website for more information on arrays.
    https://java.sun.com/docs/books/jls/...rrays.doc.html

Similar Threads

  1. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  2. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 03:29 AM
  3. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  4. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 08:23 PM
  5. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •