i created a testScores class which initialized an object with an array of scores and had an exception that if the score was <0 or >100 it would throw an IllegalArguementException. I got my main program to work it throws the exception for the array with the bad grade in it and it gives me the correct average for the array with al the good grades.
My question is how do i print the position of the element in the array that the bad grade is and print the grade that threw the exception as well??

testScores class:

public class TestScores
{
    private double[] scores;
    private double total = 0.0;

    public TestScores(double[] s)throws IllegalArgumentException
    {
        scores = s;
        for(int i = 0; i<scores.length;i++)
        {
            if(scores[i]<0 || scores[i]>100)
                throw new IllegalArgumentException("Invalid score found");
            total += scores[i];
        }
    }

    public double getAverage()
    {
        return total/scores.length;
    }
}

here is my main

import java.text.DecimalFormat;

public class TestScoresDemo
{
    public static void main(String[] args)
    {
            DecimalFormat formatter = new DecimalFormat("#0.00");

            double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0};
            double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0};
        try
        {
            TestScores bad = new TestScores(badScores);

            System.out.println("Average: " + bad.getAverage());
        }
        catch(IllegalArgumentException e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        try
        {
            TestScores good = new TestScores(goodScores);

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

        }
        catch(IllegalArgumentException e)
        {
            System.out.println(e.getMessage());
        }
    }
}

I'm not sure that I understand your requirements correctly, because surely a solution as simple as this is too easy:

if(scores[i]<0 || scores[i]>100)
    throw new IllegalArgumentException("Invalid score found: s[" + i + "] = " + scores[i]);

Even so, that seems to do what you are asking for.

bguild's solution is certainly the simplest, and is probably all you need, but for future reference here's the general way to add any amount of into to an Execption:
Create your own Exception by extending Exception, add the extra info to the constructor, have accessors for that info (or just make it public final), eg

class BadJokeException extends Exception {
    public final String theJoke, theComdian;
    public BadJokeException(String message, String theJoke, String theComdian) {
       super(message);
       this.theJoke = theJoke;
       this.theComdian = theComdian;
    }
}

... then you can create, throw, catch and query a BadJokeException just like any other Exception.

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.