0

I need the program to accept 3 test scores and then print their average, but if a score is less than -1 or greater than 100, it should throw an IllegalArgumentException. I can get the average to print out, but when testing -1 or 101, it doesn't throw the exception. What am I doing wrong?

I'm very new to learning exceptions, so any help is appreciated.

Here's my code:

import java.util.Scanner;
import java.io.*;

public class TestScores
{
public static void main(String[]args)
{
    Scanner keyboard = new Scanner(System.in);

    int[]scores = new int [3];

    System.out.println("Score 1:");
    scores[0] = keyboard.nextInt();

    System.out.println("Score 2:");
    scores[1] = keyboard.nextInt();

    System.out.println("Score 3:");
    scores[2] = keyboard.nextInt();

    int totalScores = scores[0] + scores[1] + scores[2];
    int average = 0;

    if (scores[0] >= 0 && scores[0] <= 100 || 
        scores[1] >= 0 && scores[1] <= 100 ||
        scores[2] >= 0 && scores[2] <= 100)
    {
        try
        {
            average = totalScores / 3;
        }

        catch(IllegalArgumentException e) 
        {
            System.out.println("Numbers were too low or high.");
        }

        System.out.println("Average Score: " + average);
    }



} //end of public static void



} //end of TestScores
  • 1
    How would performing arithmetic throw an `IllegalArgumentException`? You already know the numbers are valid if it reaches that point. Also, if you're not aware: the division of 2 integers yields an integer. – Josh M Feb 21 '14 at 03:55
  • I was just introduced to throwing exceptions in my class today and I don't really understand them. – user2970463 Feb 21 '14 at 03:58
  • `average = totalScores / 3;` will not throw an IllegalArgumentException. – Hot Licks Feb 21 '14 at 04:04

3 Answers3

2

The syntax is

if (condition) {
    throw new IllegalArgumentException("message here");
}
mbroshi
  • 969
  • 8
  • 21
2

You're almost there... in your if you're ensuring that all scores are within the proper range.

When the if fails, you want to throw the IllegalArgumentException in an else, like this:

if (scores[0] >= 0 && scores[0] <= 100 || 
    scores[1] >= 0 && scores[1] <= 100 ||
    scores[2] >= 0 && scores[2] <= 100)
{
    average = totalScores / 3;        
    System.out.println("Average Score: " + average);
}
else 
{
   throw new IllegalArgumentException("Numbers were too low or high.");
}
shanonvl
  • 619
  • 4
  • 6
1

It can catch an exception that the app throw an exception in the block of try.

In your block of try, we just see average = totalScores / 3;, it don't throw any exception. So it don't catch anything what was throwed.

You can use this function to throw an exception - IllegalArgumentException.

public static int getInputScore(Scanner keyboard) {
    int score = keyboard.nextInt();
    if (score < 0 || score >= 100) {
        throw new IllegalArgumentException(); 
    }
    return score;
}

And use it in main code:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    int[] scores = new int[3];

    System.out.println("Score 1:");
    try {
        scores[0] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    System.out.println("Score 2:");
    try {
        scores[1] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    System.out.println("Score 3:");
    try {
        scores[2] = getInputScore(keyboard);
    } catch (IllegalArgumentException e) {
        System.out.println("Numbers were too low or high.");
        return;
    }

    int totalScores = scores[0] + scores[1] + scores[2];
    int average = totalScores / 3;
    System.out.println("Average Score: " + average);
}
Alex Chi
  • 726
  • 6
  • 10