I am trying to havea program give me the averages from the tests. If the number is over 100 it is to give me an excpetion code this is what i have thus far... having issues on line 36-38 and 40. any help would greatly appreciated.

import java.lang.IllegalArgumentException;


//declare and initialize public class TestScores
public class TestScores

{//begin class definition
        //  ************    declare and init vars   ***********
  double average; //declare double variable: average
  double[] scoresAR;//declare double data-type array: scoresAR


    /**
     * Constructor
     * @param double[] scores - scores from TestScores class
     */
    public TestScores(double[] scores)
    {//begin class definition

        this.scoresAR = scores;//set scoresAR to the passed value of scores
        double total = 0.0;//declare and initialize total as 0.0
        for(int i = 0; i < scores.length; i++) {
            if(scores[i] <= 0.0 || scores[i] >= 100.0)
                throw new IllegalArgumentException("Scores[" + i + "] = " + scores[i]);
            total += scores[i];
        }
        average = total / scores.length;
    }
    /**
     * accessor method for average
     * @return double average;
     */
    public double getAverage()
    {//begin definition of getAverage() public method
        return this.average;//return this(current object's).average;
    }//end definition of getAverage() public method


}//end of TestScores class

and

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package testscores.draft;

/**
 *
 * @author Patrick Kemp
 */
import java.util.Scanner; //import utility scanner for getting input from user

public class TestScoresDraft {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       //   Variables

       double[] grades;
       double scoreAverage;
       grades = new double [2];
       Scanner scan = new Scanner(System.in);//create new scanner object for grabbing put from user
                System.out.print("How many scores: ");//Ask the user how many scores they want to input
                int nb = scan.nextInt();//scan the input to find the number the user typed
                double[] scores = new double[nb]; //declare and initialize scores with length of nb            
                    for(int i = 0; i < nb; ++i)//for loop will loop according to the number the user typed earlier
                    {//begin loop statements
                     System.out.print("Enter next score: ");//prompt user to input score
                 scores[i] = scan.nextDouble();//scan the user-typed input data into the scores[] array
                    }//end of loop statements

                TestScores ts = null;
                    try     {
                         ts = new TestScores(scores);
                            }
                    scoreValue = ts.getAverage(){
            }

                if(ts == null) // then one of the scores was < 0.0 or > 100.0, an invalid entry
                {
                 System.out.println("Invalid entry detected! Number must be between 0 and 100.");
                }

                scoreAverage = ts.getAverage();
                System.out.println("The average score is: " +scoreAverage);
                }
                }

    }

}

Recommended Answers

All 4 Replies

First point: never say "gives me an exception". Alwyas say exactly what the exception is and which line its on.
Second: on line 43 you check if ts is null, but that's too late. If it's null then you wil get a null pointer exception on line 40

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
  symbol:   class TestScores
  location: class testscores.draft.TestScoresDraft
    at testscores.draft.TestScoresDraft.main(TestScoresDraft.java:36)
Java Result: 1

That's your answer. You refer to a class TestScores on line 36, but nowhere do you have a definition of TestScores.

import statements rock when running into exceptions like that.

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.