hi. i need help for a problem.

i have an array of double values in the main method. i am trying to create an object and pass the array to a different class using an overloaded constructor.

i am having trouble with the syntax. please help, i need to use try/catch here to test values and then get average.

so far in main i have:

public class TestScores
{

   public static void main(String[] args)
   {

    double[] scores1 = {55.5,60.5,70.5,130.0,80.0}; // THIS SHOULD CAUSE EXCEPTION IN Scores class
        double[] scores2 = {55.5,60.5,70.5,100.0,80.0};

    Scores bs = new Scores(double[] badScores); // PROBLEM HERE.HOW DO I PASS ARRAY??

    Scores gs = new Scores(double[] goodScores); // HERE ALSO.

    System.out.println("Average: " + bs.getAverage());

      }
   }

and in the class Scores, this is what have so far, still not sure how to access the array elements from main:

public class Scores
{
    private double[] badScores;
    private double[] goodScores;
    public double average;
    public String str;
    public int LCV;


   public void Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY FROM MAIN???
   {
       badScores = bad;
       goodScores = good;

   }

   public void TestInvalid()
   {
       try
       {

                if (badScores[0] > 0 || badScores[0] < 100)
                {
                    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
                }
                else if (goodScores[0] > 0 || goodScores[0] < 100)
                {
                    average = goodScores[0]+goodScores[1]+goodScores[2]+goodScores[3]+goodScores[4]/5;
                }

       }


            catch (IllegalArgumentException ex)
            {
                str = "Invalid score found";

            }
}

   public double getAverage()
   {
       return average;
   }

   public String getInvalid()
   {
       return str;
   }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

The problem is in your Scores class. you need to have the constructorchanged to this:

public Scores(double[] bad, double[] good) // HOW TO I ACCESS ARRAY
													// FROM MAIN???
	{
		badScores = bad;
		goodScores = good;

	}

Hope this helps.

thanks. the Scores class works now.

i believe the real problem was that i was trying to pass 2 arrays as 2 separate objects while the scores class accepted it all in one statement. i am now using just one object for both arrays. thanks.

This article has been dead for over six months. Start a new discussion instead.